Advertisement
Alkhammash

Untitled

Dec 19th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.03 KB | None | 0 0
  1. <?php
  2. // Inialize session
  3. session_start();
  4.  
  5. // Check, if username session is NOT set then this page will jump to login page
  6. if (!isset($_SESSION['username'])) {
  7. header('Location: login.php');
  8. }
  9. function output_file($file, $name, $mime_type='')
  10. {
  11.  if(!is_readable($file)) die('File not found or inaccessible!');
  12.  $size = filesize($file);
  13.  $name = rawurldecode($name);
  14.  $known_mime_types=array(
  15.     "htm" => "text/html",
  16.     "exe" => "application/octet-stream",
  17.     "zip" => "application/zip",
  18.     "doc" => "application/msword",
  19.     "jpg" => "image/jpg",
  20.     "php" => "text/plain",
  21.     "xls" => "application/vnd.ms-excel",
  22.     "ppt" => "application/vnd.ms-powerpoint",
  23.     "gif" => "image/gif",
  24.     "pdf" => "application/pdf",
  25.     "txt" => "text/plain",
  26.     "html"=> "text/html",
  27.     "png" => "image/png",
  28.     "jpeg"=> "image/jpg"
  29.  );
  30.  
  31.  if($mime_type==''){
  32.      $file_extension = strtolower(substr(strrchr($file,"."),1));
  33.      if(array_key_exists($file_extension, $known_mime_types)){
  34.         $mime_type=$known_mime_types[$file_extension];
  35.      } else {
  36.         $mime_type="application/force-download";
  37.      };
  38.  };
  39.  
  40.  //turn off output buffering to decrease cpu usage
  41.  @ob_end_clean();
  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.  header('Content-Type: ' . $mime_type);
  47.  header('Content-Disposition: attachment; filename="'.$name.'"');
  48.  header("Content-Transfer-Encoding: binary");
  49.  header('Accept-Ranges: bytes');
  50.  
  51.  // multipart-download and download resuming support
  52.  if(isset($_SERVER['HTTP_RANGE']))
  53.  {
  54.     list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
  55.     list($range) = explode(",",$range,2);
  56.     list($range, $range_end) = explode("-", $range);
  57.     $range=intval($range);
  58.     if(!$range_end) {
  59.         $range_end=$size-1;
  60.     } else {
  61.         $range_end=intval($range_end);
  62.     }
  63.  
  64.     $new_length = $range_end-$range+1;
  65.     header("HTTP/1.1 206 Partial Content");
  66.     header("Content-Length: $new_length");
  67.     header("Content-Range: bytes $range-$range_end/$size");
  68.  } else {
  69.     $new_length=$size;
  70.     header("Content-Length: ".$size);
  71.  }
  72.  
  73.  /* Will output the file itself */
  74.  $chunksize = 1*(1024*1024); //you may want to change this
  75.  $bytes_send = 0;
  76.  if ($file = fopen($file, 'r'))
  77.  {
  78.     if(isset($_SERVER['HTTP_RANGE']))
  79.     fseek($file, $range);
  80.  
  81.     while(!feof($file) &&
  82.         (!connection_aborted()) &&
  83.         ($bytes_send<$new_length)
  84.           )
  85.     {
  86.         $buffer = fread($file, $chunksize);
  87.         echo($buffer);
  88.         flush();
  89.         $bytes_send += strlen($buffer);
  90.     }
  91.  fclose($file);
  92.  } else
  93.  //If no permissiion
  94.  die('Error - can not open file.');
  95.  //die
  96. die();
  97. }
  98. //Set the time out
  99. set_time_limit(0);
  100.  
  101. //path to the file
  102. $file_path='C:/wamp/www/one box/webspace/".$username."/"'.$_REQUEST['name'];
  103.  
  104.  
  105. //Call the download function with file path,file name and file type
  106. output_file($file_path, ''.$_REQUEST['name'].'', 'text/plain');
  107. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement