tito Posted June 15, 2008 Share Posted June 15, 2008 Hello, I'm trying to implement a download script with fread function. This is the original download script, without fread function: 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: 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; } 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! tito Link to comment https://forums.phpfreaks.com/topic/110338-trying-to-implement-a-download-script-with-fread-function/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.