Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


$username = "user";
$password = "password";
$server = "";
$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));
    if (connection_status()!=0) {


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.

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
thanks. modified my system to run off another internal hosted web server instead of external. – Larry G. Wapnitsky Dec 1 '11 at 14:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.