Trying to implement a download script with fread function
Posted: Sun Jun 15, 2008 12:28 pm
Hello,
I'm trying to implement a download script with fread function.
This is the original download script, without fread function:
And this is with the fread function I've tried to implement:
But it doesn't work. The download reach a max 0.5kbps and it never ends. The files that have to be downloaded are MP3 of 80/90 MB each.
I'm new here and I'm trying to learn some php, so if you can make me understand what I'm doing wrong I will really appreciate it.
Thanks so much!
rocco
I'm trying to implement a download script with fread function.
This is the original download script, without fread function:
Code: Select all
function send_file($path, $file){
# Make sure the file exists before sending headers
#-------------------------------------------------
$mainpath = "$path/$file";
$filesize2 = sprintf("%u", filesize($mainpath));
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");
sleep(1);
fpassthru($fdl);
}
return;
}
And this is with the fread function I've tried to implement:
Code: Select all
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;
}
I'm new here and I'm trying to learn some php, so if you can make me understand what I'm doing wrong I will really appreciate it.
Thanks so much!
rocco