PHP Experts, come on in

Discussion in 'OT Technology' started by Supernaut, Apr 20, 2005.

  1. Supernaut

    Supernaut New Member

    Joined:
    May 20, 2003
    Messages:
    8,047
    Likes Received:
    0
    I'm looking for a download control script that does not load the entire file into memory. I've hosted some large files for OT in the past and it kills my server every time. I've got this as an example for a chunking readfile but I don't know PHP to save my life. Can anyone throw together a script based on this?

    Code:
    <?php 
       function readfile_chunked ($filename) { 
         $chunksize = 1*(1024*1024); // how many bytes per chunk 
         $buffer = ''; 
         $handle = fopen($filename, 'rb'); 
         if ($handle === false) { 
           return false; 
         } 
         while (!feof($handle)) { 
           $buffer = fread($handle, $chunksize); 
           print $buffer; 
         } 
         return fclose($handle); 
       } 
       ?>
     
  2. Supernaut

    Supernaut New Member

    Joined:
    May 20, 2003
    Messages:
    8,047
    Likes Received:
    0
    Code:
    <?php
     function send_file($name) {
       ob_end_clean();
       $path = "./".$name;
       if (!is_file($path) or connection_status()!=0) return(FALSE);
       header("Cache-Control: no-store, no-cache, must-revalidate");
       header("Cache-Control: post-check=0, pre-check=0", false);
       header("Pragma: no-cache");
       header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
       header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
       header("Content-Type: application/octet-stream");
       header("Content-Length: ".(string)(filesize($path)));
       header("Content-Disposition: inline; filename=$name");
       header("Content-Transfer-Encoding: binary\n");
       if ($file = fopen($path, 'rb')) {
        while(!feof($file) and (connection_status()==0)) {
     	 print(fread($file, 1024*8));
     	 flush();
        }
        fclose($file);
       }
       return((connection_status()==0) and !connection_aborted());
     }
     ?>
     

Share This Page