cdsatrian

download file

Sep 10th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.83 KB | None | 0 0
  1. <?php
  2. ###############################################################
  3. # File Download
  4. ###############################################################
  5. # File name : download.php
  6. ###############################################################
  7. # Sample call:
  8. #    download.php?id=$id
  9. #
  10. ###############################################################
  11. $id=isset($_GET['id'])?$_GET['id']:'';
  12. //file path
  13. // Download folder, i.e. folder where you keep all files for download.
  14. // MUST end with slash (i.e. "/" )
  15. define('BASE_DIR','/home/~myhost/public_html/source/file/');
  16. // log downloads?  true/false
  17. define('LOG_DOWNLOADS',false);
  18. // log file name
  19. define('LOG_FILE','downloads.log');
  20. // Allowed extensions list in format 'extension' => 'mime type'
  21. // If myme type is set to empty string then script will try to detect mime type
  22. // itself, which would only work if you have Mimetype or Fileinfo extensions
  23. // installed on server.
  24. $allowed_ext = array (
  25.   /*
  26.   // archives
  27.   'zip' => 'application/zip',
  28.   'rar' => 'application/x-rar-compressed'
  29.   // documents
  30.   'pdf' => 'application/pdf',
  31.   'doc' => 'application/msword',
  32.   'xls' => 'application/vnd.ms-excel',
  33.   'ppt' => 'application/vnd.ms-powerpoint',
  34.   // executables
  35.   'exe' => 'application/octet-stream',
  36.   */
  37.   // images
  38.   'gif' => 'image/gif',
  39.   'png' => 'image/png',
  40.   'jpg' => 'image/jpeg',
  41.   'jpeg' => 'image/jpeg',
  42.   /*
  43.   // audio
  44.   'mp3' => 'audio/mpeg',
  45.   'wav' => 'audio/x-wav',
  46.   // video
  47.   'mpeg' => 'video/mpeg',
  48.   'mpg' => 'video/mpeg',
  49.   'mpe' => 'video/mpeg',
  50.   'mov' => 'video/quicktime',
  51.   'avi' => 'video/x-msvideo'
  52.   */
  53. );
  54.  
  55. //database configuration
  56. $dbhost='localhost';
  57. $dbuser='root';
  58. $dbpassword='';
  59. $dbname='test';
  60. $sql="SELECT picture FROM t_gambar WHERE id='".$id."'";
  61. $mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname);
  62. if($result =$mysqli->query($sql)){
  63.   $row=$result->num_rows;
  64.   if($row){
  65.     $data=$result->fetch_object();
  66.     $fname=$data->picture;
  67.     // Check if the file exists
  68.     // Check in subfolders too
  69.     function find_file ($dirname, $fname, &$file_path) {
  70.       $dir = opendir($dirname);
  71.       while ($file = readdir($dir)) {
  72.         if (empty($file_path) && $file != '.' && $file != '..') {
  73.           if (is_dir($dirname.'/'.$file)) {
  74.             find_file($dirname.'/'.$file, $fname, $file_path);
  75.           }
  76.           else {
  77.             if (file_exists($dirname.'/'.$fname)) {
  78.               $file_path = $dirname.'/'.$fname;
  79.               return;
  80.             }
  81.           }
  82.         }
  83.       }
  84.     } // find_file
  85.     // get full file path (including subfolders)
  86.     $file_path = '';
  87.     find_file(BASE_DIR, $fname, $file_path);
  88.     if (!is_file($file_path)) {
  89.       die("File does not exist. Make sure you specified correct file name.");
  90.     }
  91.     // file size in bytes
  92.     $fsize = filesize($file_path);
  93.     // file extension
  94.     $fext = strtolower(substr(strrchr($fname,"."),1));
  95.     // check if allowed extension
  96.     if (!array_key_exists($fext, $allowed_ext)) {
  97.       die("Not allowed file type.");
  98.     }
  99.     // get mime type
  100.     if ($allowed_ext[$fext] == '') {
  101.       $mtype = '';
  102.       // mime type is not set, get from server settings
  103.       if (function_exists('mime_content_type')) {
  104.         $mtype = mime_content_type($file_path);
  105.       }
  106.       else if (function_exists('finfo_file')) {
  107.         $finfo = finfo_open(FILEINFO_MIME); // return mime type
  108.         $mtype = finfo_file($finfo, $file_path);
  109.         finfo_close($finfo);  
  110.       }
  111.       if ($mtype == '') {
  112.         $mtype = "application/force-download";
  113.       }
  114.     }
  115.     else {
  116.       // get mime type defined by admin
  117.       $mtype = $allowed_ext[$fext];
  118.     }
  119.     // set headers
  120.     header("Pragma: public");
  121.     header("Expires: 0");
  122.     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  123.     header("Cache-Control: public");
  124.     header("Content-Description: File Transfer");
  125.     header("Content-Type: $mtype");
  126.     header("Content-Disposition: attachment; filename=\"$fname\"");
  127.     header("Content-Transfer-Encoding: binary");
  128.     header("Content-Length: " . $fsize);
  129.     // download
  130.     // @readfile($file_path);
  131.     $file = @fopen($file_path,"rb");
  132.     if ($file) {
  133.       while(!feof($file)) {
  134.         print(fread($file, 1024*8));
  135.         flush();
  136.         if (connection_status()!=0) {
  137.           @fclose($file);
  138.           die();
  139.         }
  140.       }
  141.       @fclose($file);
  142.     }
  143.     // log downloads
  144.     if (!LOG_DOWNLOADS) die();
  145.     $f = @fopen(LOG_FILE, 'a+');
  146.     if ($f) {
  147.       @fputs($f, date("m.d.Y g:ia")."  ".$_SERVER['REMOTE_ADDR']."  ".$fname."\n");
  148.       @fclose($f);
  149.     }
  150.     $query="UPDATE t_gambar SET download=download+1 WHERE id='".$id."'";
  151.     $mysqli->query($query);
  152.     $result->close();
  153.   }else{
  154.     // data not found
  155.   }
  156. } else {
  157.    // query error
  158. }
  159. $mysqli->close();
  160. ?>
Add Comment
Please, Sign In to add comment