Forum Moderators: coopster
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;
}
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;
}
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.