ClocxHD

PHP Download

Dec 29th, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.27 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Copyright 2012 Armand Niculescu - MediaDivision.com
  4.  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  5.  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  6.  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  7.  * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8.  */
  9. // get the file request, throw error if nothing supplied
  10.  
  11. // hide notices
  12. @ini_set('error_reporting', E_ALL & ~ E_NOTICE);
  13.  
  14. //- turn off compression on the server
  15. @apache_setenv('no-gzip', 1);
  16. @ini_set('zlib.output_compression', 'Off');
  17.  
  18. if(!isset($_REQUEST['file']) || empty($_REQUEST['file']))
  19. {
  20.     header("HTTP/1.0 400 Bad Request");
  21.     exit;
  22. }
  23.  
  24. // sanitize the file request, keep just the name and extension
  25. // also, replaces the file location with a preset one ('./myfiles/' in this example)
  26. $file_path  = $_REQUEST['file'];
  27. $path_parts = pathinfo($file_path);
  28. $file_name  = $path_parts['basename'];
  29. $file_ext   = $path_parts['extension'];
  30. $file_path  = '../downloads/' . $file_name;
  31.  
  32. // allow a file to be streamed instead of sent as an attachment
  33. $is_attachment = isset($_REQUEST['stream']) ? false : true;
  34.  
  35. // make sure the file exists
  36. if (is_file($file_path))
  37. {
  38.     $file_size  = filesize($file_path);
  39.     $file = @fopen($file_path,"rb");
  40.     if ($file)
  41.     {
  42.         // set the headers, prevent caching
  43.         header("Pragma: public");
  44.         header("Expires: -1");
  45.         header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
  46.         header("Content-Disposition: attachment; filename=\"$file_name\"");
  47.  
  48.         // set appropriate headers for attachment or streamed file
  49.         if ($is_attachment) {
  50.                 header("Content-Disposition: attachment; filename=\"$file_name\"");
  51.         }
  52.         else {
  53.                 header('Content-Disposition: inline;');
  54.                 header('Content-Transfer-Encoding: binary');
  55.         }
  56.  
  57.         // set the mime type based on extension, add yours if needed.
  58.         $ctype_default = "application/octet-stream";
  59.         $content_types = array(
  60.                 "exe" => "application/octet-stream",
  61.                 "zip" => "application/zip",
  62.                 "mp3" => "audio/mpeg",
  63.                 "mpg" => "video/mpeg",
  64.                 "avi" => "video/x-msvideo",
  65.         );
  66.         $ctype = isset($content_types[$file_ext]) ? $content_types[$file_ext] : $ctype_default;
  67.         header("Content-Type: " . $ctype);
  68.  
  69.         //check if http_range is sent by browser (or download manager)
  70.         if(isset($_SERVER['HTTP_RANGE']))
  71.         {
  72.             list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  73.             if ($size_unit == 'bytes')
  74.             {
  75.                 //multiple ranges could be specified at the same time, but for simplicity only serve the first range
  76.                 //http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
  77.                 list($range, $extra_ranges) = explode(',', $range_orig, 2);
  78.             }
  79.             else
  80.             {
  81.                 $range = '';
  82.                 header('HTTP/1.1 416 Requested Range Not Satisfiable');
  83.                 exit;
  84.             }
  85.         }
  86.         else
  87.         {
  88.             $range = '';
  89.         }
  90.  
  91.         //figure out download piece from range (if set)
  92.         list($seek_start, $seek_end) = explode('-', $range, 2);
  93.  
  94.         //set start and end based on range (if set), else set defaults
  95.         //also check for invalid ranges.
  96.         $seek_end   = (empty($seek_end)) ? ($file_size - 1) : min(abs(intval($seek_end)),($file_size - 1));
  97.         $seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0);
  98.      
  99.         //Only send partial content header if downloading a piece of the file (IE workaround)
  100.         if ($seek_start > 0 || $seek_end < ($file_size - 1))
  101.         {
  102.             header('HTTP/1.1 206 Partial Content');
  103.             header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$file_size);
  104.             header('Content-Length: '.($seek_end - $seek_start + 1));
  105.         }
  106.         else
  107.           header("Content-Length: $file_size");
  108.  
  109.         header('Accept-Ranges: bytes');
  110.    
  111.         set_time_limit(0);
  112.         fseek($file, $seek_start);
  113.        
  114.         while(!feof($file))
  115.         {
  116.             print(@fread($file, 1024*8));
  117.             ob_flush();
  118.             flush();
  119.             if (connection_status()!=0)
  120.             {
  121.                 @fclose($file);
  122.                 exit;
  123.             }          
  124.         }
  125.        
  126.         // file save was a success
  127.         @fclose($file);
  128.         exit;
  129.     }
  130.     else
  131.     {
  132.         // file couldn't be opened
  133.         header("HTTP/1.0 500 Internal Server Error");
  134.         exit;
  135.     }
  136. }
  137. else
  138. {
  139.     // file does not exist
  140.     header("HTTP/1.0 404 Not Found");
  141.     exit;
  142. }
  143. ?
Advertisement
Add Comment
Please, Sign In to add comment