Large file download script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sgboise
Forum Newbie
Posts: 2
Joined: Mon Aug 10, 2009 5:14 pm
Location: GamePacks.org

Large file download script

Post 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.
User avatar
zeek
Forum Commoner
Posts: 48
Joined: Mon Feb 27, 2006 7:41 pm

Re: Large file download script

Post 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);
 }
 ?>
Last edited by Benjamin on Mon Aug 10, 2009 8:24 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Large file download script

Post by Benjamin »

Can you check your error log to see why your script is failing?
Post Reply