Reputation: 159
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.
Upvotes: 0
Views: 122
Reputation: 443
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:
Upvotes: 2
Reputation: 449783
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.
Upvotes: 1
Reputation: 10880
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?
Upvotes: 1