Jump to content

Download file over FTP


e-th0mas

Recommended Posts

Hi all,

 

I'm trying to make a PHP script that downloads a file off a remote FTP server and serves the file to the visitor with a limited download speed.

 

Right now I'm using fopen like this:

 

$path = "ftp://".$username.":".$password."@".$server."/";
$fullPath = ($path.$fileName);

$speed = 400; // 400 kb/s download rate

    header("Cache-control: private");
    header("Content-Type: application/octet-stream");
    //header("Content-Length: ".filesize($fullPath));
    header("Content-Disposition: filename=\"".$fileName."\"");

    flush();

   $fd = fopen($fullPath, "r");
   while(!feof($fd)) {
         echo fread($fd, round($speed*1024));
       flush();
       sleep(1);
   }
   fclose ($fd);

 

The file does download but with the wrong speed (8kb/s), does anyone know why?

 

When I run the same code with a local file instead of a file on FTP it works fine and downloads at the speed I set it to..

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/258773-download-file-over-ftp/
Share on other sites

There might be a bottleneck downloading the file from FTP. Can you tell how fast that connection is? Is it about 8KB/s?

When you're downloading the file yourself, does the speed vary or is it constant?

 

When using the code in my first post it downloads around 8KB/s.

When I comment out

sleep(1);

it downloads at full speed, as it normally would when I download it directly myself.

However by doing this I can't limit the download speed anymore... are there any alternative ways to achieve this?

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.