etruel

User Files function getDownloads()

Aug 31st, 2011
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.94 KB | None | 0 0
  1. function getDownloads(){
  2.  
  3.     if (isset($_GET['theDLfile'])){
  4.         $file = $_GET['theDLfile'];
  5.         $upload_dir = wp_upload_dir();
  6.         global $current_user;
  7.         get_currentuserinfo();
  8.  
  9.         $url=$upload_dir['basedir'].'/file_uploads/'.$current_user->ID .'/';
  10.  
  11.         if (isset($_GET['page'])) {
  12.             if ($_GET['page']=="manage-files-main") $url=$upload_dir['basedir'].'/file_uploads/';   // the user folder is in  $file
  13.         }
  14.         $filename = $url.$file ;
  15.  
  16.         if (!file_exists($filename)) {
  17.                 die("file not found: ". $file);
  18.         }   else    {
  19.            
  20.             $fileNM = str_replace(" ","_",$file);
  21.            
  22.             $file_extension = strtolower(substr(strrchr($filename,"."),1));
  23.             switch ($file_extension) {
  24.                 case "pdf": $ctype="application/pdf"; break;
  25.                 case "exe": $ctype="application/octet-stream"; break;
  26.                 case "zip": $ctype="application/zip"; break;
  27.                 case "doc": $ctype="application/msword"; break;
  28.                 case "xls": $ctype="application/vnd.ms-excel"; break;
  29.                 case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  30.                 case "gif": $ctype="image/gif"; break;
  31.                 case "png": $ctype="image/png"; break;
  32.                 case "jpe": case "jpeg":
  33.                 case "jpg": $ctype="image/jpg"; break;
  34.                 default: $ctype="application/force-download";
  35.             }
  36.  
  37.            header("Pragma: public");
  38.             header("Expires: 0");
  39.             header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  40.             header("Cache-Control: private",false);
  41.             header("Content-Type: $ctype");
  42.             header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
  43.             header("Content-Transfer-Encoding: binary");
  44.             header("Content-Length: ".@filesize($filename));
  45.             set_time_limit(0);
  46.             @readfile("$filename") or die("File not found.");          
  47.         }
  48.     }
  49.     return;
  50. }
Add Comment
Please, Sign In to add comment