0

On my site, users can input links to files and I can stream the download process to them through my server. I use a system like this:

header('Cache-control: private');
header('Content-Type: application/octet-stream'); 
header('Content-Length: ' . $r[2]);
header('Content-Disposition: filename=' . $theName);

flush();

$file = fopen($fileName, "r");
while(!feof($file))
{
    print fread($file, 10240);  
    flush();
    sleep(1);
}
fclose($fileName);

The think is, my users downloads go pretty slowly (600kb/s). The server this is hosted on is on a 1Gbit port so they should be maxxing out their internet connection tenfold.

I'm wondering if there is a better way to do this sort of thing, maybe cURL perhaps? I don't have much experience with cURL but I'd appreciate any feedback.

Thanks.

2
  • try to print some static strings (in a loop or something) instead of reading from a file. this will help you determine if the disk access is the cause of the problem. Commented Dec 6, 2009 at 20:31
  • I think he is fetching the data live from a remote server specified by the user. Commented Dec 6, 2009 at 20:36

3 Answers 3

2

Use readfile.

If you were to insist on your approach, then it would be much more efficient this way, without flush and sleep:

while(!feof($file)) {
   print fread($file, 10240);  
}

Here's why:

  • using flush() you prevent normal buffers functioning, and
  • using sleep(1) you effectively decrease transfer speeds by pausing for 1 second every 10 KiBs.
Sign up to request clarification or add additional context in comments.

Comments

1

i am not really sure what your trying to do here .. dont see why you need that loop and specially dont see why you need the sleep() .. you should just use readfile or something like that instead of that loop , it would be much effective

Also how do you think curl will help you?

Comments

1

If it's not the one-second sleep() that @Sabeen Malik pointed out, it is most likely due to a server-side restriction imposed by your web provider (e.g. using mod_throttle or mod_bandwidth) or the web provider you are fetching data from.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.