Advertisement
droidus

Untitled

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