Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.16 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. // sanitize the file request, keep just the name and extension
  19. // also, replaces the file location with a preset one ('./myfiles/' in this example)
  20. $file_path  = 'http://www.moov.mg/medias/img/laune.20170216.PM-une.jpg';
  21. $path_parts = pathinfo($file_path);
  22. $file_name  = $path_parts['basename'];
  23. $file_ext   = $path_parts['extension'];
  24.  
  25. // allow a file to be streamed instead of sent as an attachment
  26. $is_attachment = isset($_REQUEST['stream']) ? false : true;
  27.  
  28. // make sure the file exists
  29. if (is_file($file_path))
  30. {
  31.     $file_size = filesize($file_path);
  32.     $file = @fopen($file_path, "rb");
  33.     if ($file)
  34.     {
  35.         // set the headers, prevent caching
  36.         header("Pragma: public");
  37.         header("Expires: -1");
  38.         header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
  39.         header("Content-Disposition: attachment; filename=\"$file_name\"");
  40.  
  41.         // set appropriate headers for attachment or streamed file
  42.         if ($is_attachment) {
  43.                 header("Content-Disposition: attachment; filename=\"$file_name\"");
  44.         }
  45.         else {
  46.                 header('Content-Disposition: inline;');
  47.                 header('Content-Transfer-Encoding: binary');
  48.         }
  49.  
  50.         // set the mime type based on extension, add yours if needed.
  51.         $ctype_default = "application/octet-stream";
  52.         $content_types = array(
  53.                 "exe" => "application/octet-stream",
  54.                 "zip" => "application/zip",
  55.                 "mp3" => "audio/mpeg",
  56.                 "mpg" => "video/mpeg",
  57.                 "avi" => "video/x-msvideo",
  58.         );
  59.         $ctype = isset($content_types[$file_ext]) ? $content_types[$file_ext] : $ctype_default;
  60.         header("Content-Type: " . $ctype);
  61.  
  62.         //check if http_range is sent by browser (or download manager)
  63.         if(isset($_SERVER['HTTP_RANGE']))
  64.         {
  65.             list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2);
  66.             if ($size_unit == 'bytes')
  67.             {
  68.                 //multiple ranges could be specified at the same time, but for simplicity only serve the first range
  69.                 //http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
  70.                 list($range, $extra_ranges) = explode(',', $range_orig, 2);
  71.             }
  72.             else
  73.             {
  74.                 $range = '';
  75.                 header('HTTP/1.1 416 Requested Range Not Satisfiable');
  76.                 exit;
  77.             }
  78.         }
  79.         else
  80.         {
  81.             $range = '';
  82.         }
  83.  
  84.         //figure out download piece from range (if set)
  85.         list($seek_start, $seek_end) = explode('-', $range, 2);
  86.  
  87.         //set start and end based on range (if set), else set defaults
  88.         //also check for invalid ranges.
  89.         $seek_end   = (empty($seek_end)) ? ($file_size - 1) : min(abs(intval($seek_end)),($file_size - 1));
  90.         $seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0);
  91.      
  92.         //Only send partial content header if downloading a piece of the file (IE workaround)
  93.         if ($seek_start > 0 || $seek_end < ($file_size - 1))
  94.         {
  95.             header('HTTP/1.1 206 Partial Content');
  96.             header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$file_size);
  97.             header('Content-Length: '.($seek_end - $seek_start + 1));
  98.         }
  99.         else
  100.           header("Content-Length: $file_size");
  101.  
  102.         header('Accept-Ranges: bytes');
  103.    
  104.         set_time_limit(0);
  105.         fseek($file, $seek_start);
  106.        
  107.         while(!feof($file))
  108.         {
  109.             print(@fread($file, 1024*8));
  110.             ob_flush();
  111.             flush();
  112.             if (connection_status()!=0)
  113.             {
  114.                 @fclose($file);
  115.                 exit;
  116.             }
  117.         }
  118.        
  119.         // file save was a success
  120.         @fclose($file);
  121.         exit;
  122.     }
  123.     else
  124.     {
  125.         // file couldn't be opened
  126.         header("HTTP/1.0 500 Internal Server Error");
  127.         exit;
  128.     }
  129. }
  130. else
  131. {
  132.     // file does not exist
  133.     header("HTTP/1.0 404 Not Found");
  134.     exit;
  135. }
  136. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement