Forum Moderators: coopster

Message Too Old, No Replies

Trying to implement a script with fread function

limit download speed using fread function

         

tito

12:04 am on Jun 11, 2008 (gmt 0)

10+ Year Member



Hello,

I'm trying to implement a download script with fread function, this is what I'm doing, but it doesn't work. Please what I'm missing? Thanks in advance!

# Send (download) file via pass thru
#-------------------------------------
function send_file($path, $file){

# Make sure the file exists before sending headers
#-------------------------------------------------
$mainpath = "$path/$file";
$filesize2 = sprintf("%u", filesize($mainpath));

$speed = 50; // i.e. 50 kb/s download rate

if(!$fdl=@fopen($mainpath,'r')){
#include ("$header");
print "<p>ERROR - Invalid Request (Downloadable file Missing or Unreadable)</p><br /><br />";
die;
}else{
set_time_limit(0);
# Send the headers then send the file
#------------------------------------
header("Cache-Control: ");# leave blank to avoid IE errors
header("Pragma: ");# leave blank to avoid IE errors
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$file."\"");
header("Content-length:".(string)($filesize2));
#header("Content-Length: ".$filesize2);
#header("Content-Length: $filesize2");

flush();
$fdl = fopen($file, "r");
while(!feof($fdl)) {
echo fread($fdl, round($speed*1024)); // $speed kb at a time
flush();

sleep(1);
fpassthru($fdl);
}
return;
}

PHP_Chimp

8:59 pm on Jun 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the error you are getting?

If something isnt working then get rid of all of the error suppression.
if(!$fdl=@fopen($mainpath,'r')){ should be if(!$fdl=fopen($mainpath,'r')){ , as the @ is masking the fopen errors.

tito

2:27 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



Hello,

the error I'm getting with my first attempt is

Parse error: syntax error, unexpected $end in /home/.sites/28/site1/web/podcasts-archive/index.php on line 1322

if I remove "@" I get the same error, but on line 1319. that's where "@" should be.

eelixduppy

2:54 pm on Jun 12, 2008 (gmt 0)



You have a problem with your braces ('}' and '{'). Check to make sure that for every one you open, it has a closing brace. Also make sure that these closing braces are in the correct places.

tito

8:47 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



Thanks eelixduppy, brace closed and it's fine.

however I'm still a bit confused because if I leave "@" or take it away, as PHP_Chimp said, it makes no difference. It's not clear to me what such sign means, anyway I took it away.

Also I believe that fread function is not precise, because I've tested it and here's what I've got:

with FF 2.0.0.14, it reduces the download to a maximum 80/100 kbps, no matter if I set $speed = 50; or 5
with IE 6 the download it's maybe a little bit faster,

but anyway it's reduced somehow, without the fread function download are going up to 300/400kbps, of course depending on the dwnld speed of my internet connection.

Please, what do you think about? take it or leave it the "@"? Is the fread function an unprecise function anyway?
here's the code I've tested and using right now
Thanks so much!

# Send (download) file via pass thru
#-------------------------------------
function send_file($path, $file){

# Make sure the file exists before sending headers
#-------------------------------------------------
$mainpath = "$path/$file";
$filesize2 = sprintf("%u", filesize($mainpath));

$speed = 5; // i.e. 50 kb/s download rate

if(!$fdl=fopen($mainpath,'r')){
#include ("$header");
print "<p>ERROR - Invalid Request (Downloadable file Missing or Unreadable)</p><br /><br />";
die;
}else{
set_time_limit(0);
# Send the headers then send the file
#------------------------------------
header("Cache-Control: ");# leave blank to avoid IE errors
header("Pragma: ");# leave blank to avoid IE errors
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$file."\"");
header("Content-length:".(string)($filesize2));
#header("Content-Length: ".$filesize2);
#header("Content-Length: $filesize2");

flush();
$fdl = fopen($file, "r");
while(!feof($fdl)) {
echo fread($fdl, round($speed*1024)); // $speed kb at a time
flush();
}
sleep(1);
fpassthru($fdl);
}
return;
}

tito

3:24 am on Jun 13, 2008 (gmt 0)

10+ Year Member



one more doubt: I thought that maybe the brace had to be placed after "sleep(1);" and not after the 2nd "flush();". I've tried but the download appeared to be stucked at max 0.5kbps.

Then I've placed back the code as in the example above and tested it once again. This time I've noticed that the download, while going to the end, reached up 400kbps abt.
These dwnlds I'm testing are 80/90 MB each one,

I'm sure there must be something in the code that doesn't let the fread function works properly, but unfortunately I'm clueless.

tito

8:44 pm on Jun 13, 2008 (gmt 0)

10+ Year Member



I've noticed that the downloaded mp3 file it's damaged and cannot be played.