Guest User

Crazy

a guest
Jul 24th, 2009
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. <?php
  2. //turn off error reporting(wouldn't want to make our download currupt
  3. error_reporting(0);
  4.  
  5. function output_file($file, $name, $mime_type='')
  6. {
  7.  if(!is_readable($file)) die('File not found or inaccessible!');
  8.  
  9.  $size = filesize($file);
  10.  $name = rawurldecode($name);
  11.  
  12.  /* Figure out the MIME type (if not specified) */
  13.  $known_mime_types=array(
  14.     "pdf" => "application/pdf",
  15.     "txt" => "text/plain",
  16.     "html" => "text/html",
  17.     "htm" => "text/html",
  18.     "exe" => "application/octet-stream",
  19.     "zip" => "application/zip",
  20.     "rar" => "application/x-rar",
  21.     "doc" => "application/msword",
  22.     "xls" => "application/vnd.ms-excel",
  23.     "ppt" => "application/vnd.ms-powerpoint",
  24.     "gif" => "image/gif",
  25.     "png" => "image/png",
  26.     "jpeg"=> "image/jpg",
  27.     "jpg" =>  "image/jpg",
  28.     "php" => "text/plain"
  29.  );
  30.  
  31.  if($mime_type==''){
  32.      $file_extension = strtolower(substr(strrchr($file,"."),1));
  33.  
  34.      if(array_key_exists($file_extension, $known_mime_types)){
  35.         $mime_type=$known_mime_types[$file_extension];
  36.      } else {
  37.         $mime_type="application/force-download";
  38.      };
  39.  };
  40.  
  41.  @ob_end_clean(); //turn off output buffering to decrease cpu usage
  42.  
  43.  // required for IE, otherwise Content-Disposition may be ignored
  44.  if(ini_get('zlib.output_compression'))
  45.   ini_set('zlib.output_compression', 'Off');
  46.  
  47.  header('Content-Type: ' . $mime_type);
  48.  header('Content-Disposition: attachment; filename="'.$name.'"');
  49.  header("Content-Transfer-Encoding: binary");
  50.  header('Accept-Ranges: bytes');
  51.  
  52.  /* The three lines below basically make the
  53.     download non-cacheable */
  54.  header("Cache-control: private");
  55.  header('Pragma: private');
  56.  header("Expires: Mon, 1 Jul 2000 00:00:00 GMT");
  57.  
  58.  // multipart-download and download resuming support
  59.  if(isset($_SERVER['HTTP_RANGE']))
  60.  {
  61.     list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
  62.     list($range) = explode(",",$range,2);
  63.     list($range, $range_end) = explode("-", $range);
  64.     $range=intval($range);
  65.     if(!$range_end) {
  66.         $range_end=$size-1;
  67.     } else {
  68.         $range_end=intval($range_end);
  69.     }
  70.  
  71.     $new_length = $range_end-$range+1;
  72.     header("HTTP/1.1 206 Partial Content");
  73.     header("Content-Length: $new_length");
  74.     header("Content-Range: bytes $range-$range_end/$size");
  75.  } else {
  76.     $new_length=$size;
  77.     header("Content-Length: ".$size);
  78.  }
  79.  
  80.  /* output the file itself */
  81.  $chunksize = 1024; //you may want to change this - I use it for big files
  82.  $bytes_send = 0;
  83.  if ($file = fopen($file, 'r'))
  84.  {
  85.     if(isset($_SERVER['HTTP_RANGE']))
  86.     fseek($file, $range);
  87.  
  88.     while(!feof($file) &&
  89.         (!connection_aborted()) &&
  90.         ($bytes_send<$new_length)
  91.           )
  92.     {
  93.         $buffer = fread($file, $chunksize);
  94.         print($buffer); //echo($buffer); // is also possible
  95.         flush();
  96.         $bytes_send += strlen($buffer);
  97.     }
  98.  fclose($file);
  99.  } else die('Error - can not open file.');
  100.  
  101. die();
  102. }
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment