irwan

downloadphp

Mar 15th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.68 KB | None | 0 0
  1. <?php
  2.  
  3. define('ALLOWED_REFERRER', '');
  4. define('BASE_DIR','/home/user/downloads/');
  5. define('LOG_DOWNLOADS',true);
  6.  
  7. // log file name
  8. define('LOG_FILE','downloads.log');
  9.  
  10. // Allowed extensions list in format 'extension' => 'mime type'
  11. // If myme type is set to empty string then script will try to detect mime type
  12. // itself, which would only work if you have Mimetype or Fileinfo extensions
  13. // installed on server.
  14. $allowed_ext = array (
  15.  
  16.   // archives
  17.   'zip' => 'application/zip',
  18.  
  19.   // documents
  20.   'pdf' => 'application/pdf',
  21.   'doc' => 'application/msword',
  22.   'xls' => 'application/vnd.ms-excel',
  23.   'ppt' => 'application/vnd.ms-powerpoint',
  24.  
  25.   // executables
  26.   'exe' => 'application/octet-stream',
  27.  
  28.   // images
  29.   'gif' => 'image/gif',
  30.   'png' => 'image/png',
  31.   'jpg' => 'image/jpeg',
  32.   'jpeg' => 'image/jpeg',
  33.  
  34.   // audio
  35.   'mp3' => 'audio/mpeg',
  36.   'wav' => 'audio/x-wav',
  37.  
  38.   // video
  39.   'mpeg' => 'video/mpeg',
  40.   'mpg' => 'video/mpeg',
  41.   'mpe' => 'video/mpeg',
  42.   'mov' => 'video/quicktime',
  43.   'avi' => 'video/x-msvideo'
  44. );
  45.  
  46.  
  47.  
  48. ####################################################################
  49. ###  DO NOT CHANGE BELOW
  50. ####################################################################
  51.  
  52. // If hotlinking not allowed then make hackers think there are some server problems
  53. if (ALLOWED_REFERRER !== ''
  54. && (!isset($_SERVER['HTTP_REFERER']) || strpos(strtoupper($_SERVER['HTTP_REFERER']),strtoupper(ALLOWED_REFERRER)) === false)
  55. ) {
  56.   die("Internal server error. Please contact system administrator.");
  57. }
  58.  
  59. // Make sure program execution doesn't time out
  60. // Set maximum script execution time in seconds (0 means no limit)
  61. set_time_limit(0);
  62.  
  63. if (!isset($_GET['f']) || empty($_GET['f'])) {
  64.   die("Please specify file name for download.");
  65. }
  66.  
  67. // Nullbyte hack fix
  68. if (strpos($_GET['f'], "\0") !== FALSE) die('');
  69.  
  70. // Get real file name.
  71. // Remove any path info to avoid hacking by adding relative path, etc.
  72. $fname = basename($_GET['f']);
  73.  
  74. // Check if the file exists
  75. // Check in subfolders too
  76. function find_file ($dirname, $fname, &$file_path) {
  77.  
  78.   $dir = opendir($dirname);
  79.  
  80.   while ($file = readdir($dir)) {
  81.     if (empty($file_path) && $file != '.' && $file != '..') {
  82.       if (is_dir($dirname.'/'.$file)) {
  83.         find_file($dirname.'/'.$file, $fname, $file_path);
  84.       }
  85.       else {
  86.         if (file_exists($dirname.'/'.$fname)) {
  87.           $file_path = $dirname.'/'.$fname;
  88.           return;
  89.         }
  90.       }
  91.     }
  92.   }
  93.  
  94. } // find_file
  95.  
  96. // get full file path (including subfolders)
  97. $file_path = '';
  98. find_file(BASE_DIR, $fname, $file_path);
  99.  
  100. if (!is_file($file_path)) {
  101.   die("File does not exist. Make sure you specified correct file name.");
  102. }
  103.  
  104. // file size in bytes
  105. $fsize = filesize($file_path);
  106.  
  107. // file extension
  108. $fext = strtolower(substr(strrchr($fname,"."),1));
  109.  
  110. // check if allowed extension
  111. if (!array_key_exists($fext, $allowed_ext)) {
  112.   die("Not allowed file type.");
  113. }
  114.  
  115. // get mime type
  116. if ($allowed_ext[$fext] == '') {
  117.   $mtype = '';
  118.   // mime type is not set, get from server settings
  119.   if (function_exists('mime_content_type')) {
  120.     $mtype = mime_content_type($file_path);
  121.   }
  122.   else if (function_exists('finfo_file')) {
  123.     $finfo = finfo_open(FILEINFO_MIME); // return mime type
  124.     $mtype = finfo_file($finfo, $file_path);
  125.     finfo_close($finfo);  
  126.   }
  127.   if ($mtype == '') {
  128.     $mtype = "application/force-download";
  129.   }
  130. }
  131. else {
  132.   // get mime type defined by admin
  133.   $mtype = $allowed_ext[$fext];
  134. }
  135.  
  136. // Browser will try to save file with this filename, regardless original filename.
  137. // You can override it if needed.
  138.  
  139. if (!isset($_GET['fc']) || empty($_GET['fc'])) {
  140.   $asfname = $fname;
  141. }
  142. else {
  143.   // remove some bad chars
  144.   $asfname = str_replace(array('"',"'",'\\','/'), '', $_GET['fc']);
  145.   if ($asfname === '') $asfname = 'NoName';
  146. }
  147.  
  148. // set headers
  149. header("Pragma: public");
  150. header("Expires: 0");
  151. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  152. header("Cache-Control: public");
  153. header("Content-Description: File Transfer");
  154. header("Content-Type: $mtype");
  155. header("Content-Disposition: attachment; filename=\"$asfname\"");
  156. header("Content-Transfer-Encoding: binary");
  157. header("Content-Length: " . $fsize);
  158.  
  159. // download
  160. // @readfile($file_path);
  161. $file = @fopen($file_path,"rb");
  162. if ($file) {
  163.   while(!feof($file)) {
  164.     print(fread($file, 1024*8));
  165.     flush();
  166.     if (connection_status()!=0) {
  167.       @fclose($file);
  168.       die();
  169.     }
  170.   }
  171.   @fclose($file);
  172. }
  173.  
  174. // log downloads
  175. if (!LOG_DOWNLOADS) die();
  176.  
  177. $f = @fopen(LOG_FILE, 'a+');
  178. if ($f) {
  179.   @fputs($f, date("m.d.Y g:ia")."  ".$_SERVER['REMOTE_ADDR']."  ".$fname."\n");
  180.   @fclose($f);
  181. }
  182.  
  183. ?>
Add Comment
Please, Sign In to add comment