0

I'm trying to make a download button on my webpage, using FTP. But when I try to do this, the downloaded file is saved onto the machine which hosts my website, and not the users PC, which I want. Isn't it possible to make it, so the file can be downloaded as normal from the FTP like in the image below?

enter image description here

This is the download FTP function I have right now, which downloads the file into my websites project folder..


function FTP_download($id){
    ob_end_flush();
    $remote_file = $id.'.log';
    $local_file = $id.'.log';

    $conn_id = ftp_connect($this->ftp_server);
    $login_result = ftp_login($conn_id, $this->ftp_user_name, $this->ftp_user_pass);

    // try to download $server_file and save to $local_file
    if (ftp_get($conn_id, $local_file, $remote_file, FTP_BINARY)) {
        echo "Successfully written to $local_file\n";
    } else {
        echo "There was a problem\n";
    }

    // close the connection
    ftp_close($conn_id);
}
  • See https://www.php.net/manual/en/function.readfile.php. That isn't FTP though. You can't change the protocol the request was made over. – user3783243 Dec 23 '21 at 14:09
  • @user3783243 Aaah I see, so I have to make a download `.php` file and include some specific `headers` and then use the `readfile` function to create the download? :) – Mads Sander Høgstrup Dec 23 '21 at 14:12
  • 1
    You don't need a separate file necessarily, but yes you need to make PHP forward the file on to the client – ADyson Dec 23 '21 at 14:14
  • @ADyson Just tried the `readfile` and it outputs the content of my `text file`, so I guess I do have to make some kind of another file with some download `headers?` – Mads Sander Høgstrup Dec 23 '21 at 14:15
  • You can put the header calls in this file. That tells the browser what to do. Natively you are saying it is HTML so the browser renders it as such. – user3783243 Dec 23 '21 at 14:16
  • @user3783243 yeah then I need another `ftp_download.php` file, in my case :) since the file I use right now, contains some webpage stuff. In other words have a `webpage.php` and a `ftp_download.php` file – Mads Sander Høgstrup Dec 23 '21 at 14:17
  • You have basically asked this already and we have already pointed you to [Retrieve image from FTP to webpage](https://stackoverflow.com/q/17169251/850848). There's no difference between implementing a download/display link to an image or to any other file. – Martin Prikryl Dec 23 '21 at 14:25
  • @MartinPrikryl yes but no, because i tried to do it like that but didn't know I had to change some things :) I've written a solution now for this problem – Mads Sander Høgstrup Dec 23 '21 at 14:38

1 Answers1

0

So I've made a solution for download my .log files

FTP_download.php

...
$file = $_GET['file'].'.log';
// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// get the size of $file
$size = ftp_size($conn_id, $file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . $size);
readfile('ftp://'.$ftp_user_name.':'.urlencode($ftp_user_pass).'@'.$ftp_server.'/'.$file);
exit;

and I have a function with a download link to this .php page

<?php
function FTP_download($id){
    ?> <a href="ftp_download.php?file=<?php echo $id; ?>" download> Download logfile</a> <?php
}
?>
  • 1
    Once you have logged in anyway, using `ftp://` is inefficient, as you unnecessarily create another connection, use `ftp_get($conn_id, "php://output", $file, FTP_BINARY);` as shown in https://stackoverflow.com/q/47240635/850848 – Martin Prikryl Dec 23 '21 at 15:08
  • Should I just remove the connect/login part then? – Mads Sander Høgstrup Dec 28 '21 at 08:28