I have the following script that works on a local system, but not on a web-hosted system (where it needs to be). Locally, it downloads the file with no issues. When hosted, I get either zero- or low-byte files instead of the entire file.
<?php
$username = "user";
$password = "password";
$server = "ftp.domain.com";
$path = $_GET['p'];
$filename = $_GET['f'];
$file_full = $path."/".$filename;
$file_path = "ftp://".$username.":".$password."@".$server."/".$file_full;
$fsize = filesize($file_path);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
//header("Content-Type: $mtype");
header("Content-Type: application/octet-stream");
//header("Content-Disposition: attachment; filename=\"$asfname\""); */
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
// download
// @readfile($file_path);
$file = @fopen($file_path,"rb");
if ($file) {
while(!feof($file)) {
print(fread($file, 1024*8));
flush();
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
?>
My host provider is Hostmonster (if that helps at all).
In case you're wondering, I know I can just download links via FTP, but I need to force the user to choose whether to "Save As" or "Open", hence the script.
You're trying to stream/forward remote FTP content through your web server; a dozen things can go wrong there.
The most obvious one is if the FTP machine does not support retrieving the file size and the computed fsize is incorrect.
Let's just say that this is pretty much never the right solution.