Sie sind auf Seite 1von 4

PHP Script For Download All type Of File From Website opensourceprogrammer.

in
0 1 year ago by admin in PHP For download a file in PHP we need PHP script. In website by giving complete path to that file it can be download but not all type of file some file cant download using direct path to that file like server script file, Images, mp3 files or pdf file etc. and another reason of security if some get direct to that file folder it cant download any other file. For download file and secure the location in PHP there is a few line of PHP Code that can use for download. Few Tips need to consider in writing a download code 1. 2. 3. 4. 5. 6. Check File exist at given location. Check file type then select content-type header according to that file type. Check site of file that want to download. Set Script Execution time limit to unlimited. Set page expire to 0. Set the name for download file (by name file save to disk).

? 01 02 03 04 public function getExtension($filename){ 05 06 07 08 $filetxtArr = explode(.,$filename); 09 $ext = end($filetxtArr); 10 11 return strtolower($ext); 12 13 } 14 15 16 17 public function getfilecontenttype($ext){ 18 19 switch ($ext) { 20 case "pdf": $ctype="application/pdf"; 21break; case "exe": $ctype="application/octet22 stream"; break; 23 case "zip": $ctype="application/zip"; 24break; 25 case "doc": 26$ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms27 excel"; break; 28 case "ppt": $ctype="application/vnd.ms29powerpoint"; break; case "gif": $ctype="image/gif"; break; 30 case "png": $ctype="image/png"; break; 31 case "jpeg": 32 case "jpg": $ctype="image/jpg"; break; 33 default: $ctype="application/force34download"; } 35 36 return $ctype; 37 38 } 39 40 41 42 public function download($filepath,$file){ 43 44 45
Class downloadfile{

$path = $_SERVER[DOCUMENT_ROOT].$filepath; 46 47 $completepath = $path.$file; 48 49 if( file_exists($completepath) ){ 50 51 $file_size = filesize($completepath); 52 53 $fileext = $this-> 54getExtension($file); 55 $filectype = $this-> 56 getfilecontenttype($fileext); 57 58 $save_as_name = basename($file); // it 59can be some define name 60 61 ini_set('session.cache_limiter', ''); 62 63header('Expires: Thu, 19 Nov 1981 08:52:00 GMT'); 64 65header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 66 header('Pragma: no-cache'); 67 68header("Content-Type: $filectype"); //defile that we work with file 69 70header("Content-Transfer-Encoding: binary"); 71header("Content-Length: ".$file_size); 72 73header("Content-Disposition: disposition-type=attachment; 74filename=\"$save_as_name\""); 75 } 76 77 } 78 }

File :- download.php ? 1$filename = $_GET[filename]; 2 $objdown = new downloadfile(); 3 4$objdown->download($filepath,$filename); 5 6 7 8<a href=download.php?filename = file.pdf>Download</a>

Das könnte Ihnen auch gefallen