Jump to content

Trying to implement a download script with fread function


tito

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.