Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:16

0001 <?php
0002 
0003 /*
0004  * download.php
0005  *
0006  * Send a file to the browser, forcing a download dialogue
0007  * Credit given below
0008  *
0009  */
0010 
0011 /*
0012 From php.net readline function page
0013 
0014 m (at) mindplay (dot) dk
0015 16-Jan-2005 09:20
0016 Here's a function for sending a file to the client - it may look more
0017 complicated than necessary, but has a number of advantages over
0018 simpler file sending functions: - Works with large files, and uses
0019 only an 8KB buffer per transfer.  - Stops transferring if the client
0020 is disconnected (unlike many scripts, that continue to read and buffer
0021 the entire file, wasting valuable resources) but does not halt the
0022 script - Returns TRUE if transfer was completed, or FALSE if the
0023 client was disconnected before completing the download - you'll often
0024 need this, so you can log downloads correctly.  - Sends a number of
0025 headers, including ones that ensure it's cached for a maximum of 2
0026 hours on any browser/proxy, and "Content-Length" which most people
0027 seem to forget.  (tested on Linux (Apache) and Windows (IIS5/6) under
0028 PHP4.3.x)
0029 
0030 Note that the folder from which protected files will be pulled, is set
0031 as a constant in this function (/protected) ... Now here's the
0032 function:
0033 */
0034 
0035 
0036 function send_file($path) {
0037   ob_end_clean();
0038   if (preg_match(':\.\.:', $path)) { return(FALSE); }
0039   if (! preg_match(':^plotcache/:', $path)) { return(FALSE); }
0040   if (!is_file($path) or connection_status()!=0) return(FALSE);
0041 
0042   header("Cache-Control: no-store, no-cache, must-revalidate");
0043   header("Cache-Control: post-check=0, pre-check=0", false);
0044   header("Pragma: no-cache");
0045   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");
0046   header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
0047   header("Content-Type: application/octet-stream");
0048   header("Content-Length: ".(string)(filesize($path)));
0049   header("Content-Disposition: inline; filename=".basename($path));
0050   header("Content-Transfer-Encoding: binary\n");
0051   if ($file = fopen($path, 'rb')) {
0052     while(!feof($file) and (connection_status()==0)) {
0053       print(fread($file, 1024*8));
0054       flush();
0055       @ob_flush();
0056     }
0057     fclose($file);
0058   }
0059   return((connection_status()==0) and !connection_aborted());
0060 }
0061 ?>
0062 
0063 <?php 
0064 if (!isset($_GET['file'])) { exit; }
0065 
0066 if (!send_file($_GET['file'])) { 
0067   die ("File transfer failed"); 
0068 // either the file transfer was incomplete 
0069 // or the file was not found 
0070 } else { 
0071 // the download was a success 
0072 // log, or do whatever else 
0073 } 
0074 ?>