Page 1 of 1

Large file download script

Posted: Mon Aug 10, 2009 5:16 pm
by sgboise
Hello,

I'm using a script that allows my clients to download files from my store. The problem I'm running into is that for some reason the download stops at around 5 minutes. I'm guessing it's hitting the max_execute_limit set by the web host.

Below is the code that I'm currently using. Can anyone suggestion a different method to allow people to download large files?

What I think would be if I could redirect the user to the actual file once I validate that the person has access to the file but that would mean giving the person direct access to the folder which I don't want to do.

Code: Select all

function readfile_chunked($file_name,$ret_bytes=true) {
    set_time_limit(0);
    $memory_limit = 1;
    $chunksize = $memory_limit*(1024*1024);
    $buffer = '';
    $cnt =0;
    $handle = fopen($file_name, 'rb');
   if ($handle === false) {
       return false;
   }
   while (!feof($handle)) {
       $buffer = fread($handle, $chunksize);
       echo $buffer;
       ob_flush();
       flush();
       if ($retbytes) {
           $cnt += strlen($buffer);
       }
   }
       $status = fclose($handle);
       exit;
   if ($retbytes && $status) {
       return $cnt;
   }
   return $status;
}
Thanks in advance.

Re: Large file download script

Posted: Mon Aug 10, 2009 8:22 pm
by zeek
Simply have PHP return file headers similar to the following:

(example taken from:http://www.ryboe.com)

Code: Select all

 
<?PHP
 // Define the path to file
 $file = 'ryboe_tag_cloud.zip';
 
 if(!file)
 {
     // File doesn't exist, output error
     die('file not found');
 }
 else
 {
     // Set headers
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=$file");
     header("Content-Type: application/zip");
     header("Content-Transfer-Encoding: binary");
    
     // Read the file from disk
     readfile($file);
 }
 ?>

Re: Large file download script

Posted: Mon Aug 10, 2009 8:24 pm
by Benjamin
Can you check your error log to see why your script is failing?