Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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

 
discard

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.