For a web solution I am currently involved in I had a challenge where files from a FTP-server should be downloaded from the webserver. Thanks to a solution provided by NogDog to cesarcesar @ CodingForums.org in 2007 I managed to solve this.
This is the solution:
First you connect to your ftp-server
$connection = ftp_connect($ftp_server);
$login = ftp_login($connection, $ftp_user_name, $ftp_password);
You then check if it was possible to log in. If it wasn’t, you have to end the script.
if (!$connection || !$login) {
die('Connection attempt failed!');
}
I then created a local directory (/temp/) where I want these files to be downloaded. Then I created a filename.
For my solution I am getting most values from a database, so here I am only creating a example filename
$localfile = "./temp/example.doc";
$remotefile = "/dir/seconddir/example.doc";
Now we do the actual download and at the same time the download to the visitor:
if (ftp_get($connection, $localfile, $remotefile, FTP_BINARY)){
header('Content-Length: '. filesize($localfile));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($localfile).'"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile($localfile); // send the file
exit; // make sure no extraneous characters get appended
}
The file will be stored locally, and since you don’t know when the user has gotten the file, set up a cronjob to remove downloaded files so that your area doesn’t get filled with files you don’t need.
What you also can do, is to check if the file is downloaded already.
Link to original post
I hope this was of help for someone.

