Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.