1

How can I make to link the echo's of "Ficheiro" with a script to download the "Ficheiro" filename?

Example echo's from "Ficheiro" will be something like 1509071717-de_dust2.dem

How can I download this file from this ftp?

Ftp: ftp://USER:PASSWORD@IPHOST:PORT/

Code:

<?php
require('config.php');

$iConnection = mysqli_connect($_HOST_,$_USER_,$_PASS_,$_MYDB_) or die("<link rel='stylesheet' type='text/css' href='css/style.css'><h4>Rank not found.</h4>");
?>

<html>
 <body>
   <table width="100%" border="0" cellpadding="1" cellspacing="1">
   <tr>
    <th>FICHEIRO</th>
   </tr>
 <?php
   $sql = "SELECT * FROM HLTV1 ORDER BY Ficheiro DESC";
   $iResult = mysqli_query($iConnection, $sql);
   while($Row = mysqli_fetch_array($iResult))
  
   echo "<td style='text-align: center;' width='5%'>" . $Row['Ficheiro'] . "</td>";
   echo "</tr>";
  
  mysqli_close($iConnection);
   ?>
 </table>
 </body>
</html>
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
v4N3k
  • 11
  • 1

1 Answers1

0

If you do not care about clear ftp username/password, you can simply put

echo "<td style='...'><a href='ftp://USER:PASSWORD@UPHOST:port/" . $Row['Fichieiro'] . "'>" . $Row['Fichieiro'] . "</a></td>";

If you want to hide that you must create a separate script that will download the file from FTP and output it directly for the user to download. Take a look here for an example (replace http with ftp). Note that this assumes you have allow_url_fopen and related in your PHP.ini. The other variant will be with curl (and again, you must have it installed and enabled).

Here is an example:

echo "<td style='...'><a href='donwload.php?file=" . $Row['Fichieiro'] . "'>" . $Row['Fichieiro'] . "</td>";

And in the download.php file you might have:

$file = (!empty($_GET['file']) ? $_GET['file'] : null);
if($file) {
    try {
        $fp = fopen('ftp://USER:PASSWORD@UPHOST:port/' . $file, 'rb');

        // send the right headers
        header("Content-Type: application/x-download");
        header('Content-Disposition: attachment; filename="' . $file . '"');

        // Dump the file directly
        fpassthru($fp);

        // exit to avoid some extra spaces in output
        exit;
    } catch (\Exception $ex) {
        echo "Error, file not found (most probably)";
    }
}

echo "Error, no file specified";

This way, the only thing the user sees is the name of the file. I am using fpassthru here for the sake of simplicity, but you can use fopen, fread, fwrite and fclose for a cleaner code. For an example with these functions, take a look here.

Hope that helps.

Community
  • 1
  • 1
Alex
  • 810
  • 9
  • 16
  • Alex your code works but with your code my ftp details will be seen by all. Can you make me a code to download from ftp but ommiting the ftp user and pass? – v4N3k Sep 07 '15 at 20:45
  • Perfect Alex :) Thank you men ;) – v4N3k Sep 07 '15 at 21:47