首頁 > 後端開發 > php教程 > 不錯的PHP檔案下載類

不錯的PHP檔案下載類

WBOY
發布: 2016-07-25 08:58:46
原創
1313 人瀏覽過
複製程式碼
  1. /**
  2. ** php檔案下載類別
  3. **範例:
  4. $download=new download('php,exe,html',false);
  5. if(!$download->downloadfile($filename ))
  6. {
  7. echo $download->geterrormsg();
  8. }
  9. * by bbs.it-home.org
  10. */
  11. 類別下載{
  12. var $debug=true ;
  13. var $errormsg='';
  14. var $Filter=array();
  15. var $filename='';
  16. var $mineType='text/plain';
  17. var $ xlq_filetype= array();
  18. 函數下載($fileFilter='',$isdebug=true)

  19. {
  20. $this->setFilter($fileFilter);
  21. $this->setdebug($isdebug);
  22. $this->setfiletype();
  23. }
  24. 函數 setFilter($fileFilter)

  25. {
  26. if(empty($fileFilter)) return ;
  27. $this->Filter=explode(',',strtolower($fileFilter));
  28. }
  29. function setdebug($debug )
  30. {
  31. $this->debug=$debug;
  32. }
  33. function setfilename($filename)

  34. {
  35. $this->filename=$filename;
  36. }
  37. 函數downloadfile($filename)

  38. {
  39. $this->setfilename($filename);
  40. if($this->filecheck())
  41. {
  42. $fn = array_pop(explode( '/', strtr( $this->filename, '\', '/' ) ) );
  43. header( "Pragma: public" );
  44. header( "過渡時間:0" ); // 設定過渡時間
  45. header( "Cache-Component: Must-revalidate, post-check=0, pre-check=0" );
  46. header( "Content-type:".$this->; mineType );
  47. header( "內容長度: " .filesize( $this->filename ) );
  48. header( "內容處理:附件; filename="$fn"" );
  49. header( '內容傳輸編碼:二進位' );
  50. readfile( $this->filename );
  51. 回傳true;
  52. }else
  53. {
  54. 回傳false;
  55. }
  56. }
  57. 函數geterrormsg()
  58. {
  59. 回傳$this->errormsg;
  60. }
  61. 函數filecheck()

  62. {
  63. $filename=$this ->檔名;
  64. if(file_exists($filename))
  65. {
  66. $filetype=strtolower(array_pop(explode('.',$filename))) ;
  67. if(in_array($ filetype) ,$this->Filter))
  68. {
  69. $this->errormsg.=$filename.'不允許下載! '不允許下載!') ;
  70. return false;
  71. }else
  72. {
  73. if ( function_exists( "mime_content_type" ) )
  74. {
  75. $this->mineType = mime_content_type( $name );
  76. }
  77. if(empty($this->mineType))
  78. {
  79. if( isset($this->xlq_filetype[$filetype]) ) $this->mineType = $this->xlq_filetype [$filetype];
  80. }
  81. if(!empty($this->mineType))
  82. return true;
  83. else
  84. {
  85. $this->errormsg.='取得'.$filename.'檔案類型時候發生錯誤,或不存在預定檔案類型內';
  86. if($this->debug) exit('取得檔案類型出錯');
  87. return false;
  88. }
  89. }
  90. }else
  91. {
  92. $this->errormsg.=$filename.'不存在!';
  93. if($this->debug) exit($filename. '不存在!') ;
  94. return false;
  95. }
  96. }
  97. function setfiletype()

  98. {
  99. $this->xlq_filetype['chm']='application/octet-stream';
  100. $this->xlq_filetype[ ' ppt']='application/vnd.ms-powerpoint';
  101. $this->xlq_filetype['xls']='application/vnd.ms-excel';
  102. $this-> ;xlq_filetype[' doc ']='application/msword';
  103. $this->xlq_filetype['exe']='application/octet-stream';
  104. $this->xlq_filetype['rar']='application/octet - stream';
  105. $this->xlq_filetype['js']="javascript/js";
  106. $this->xlq_filetype['css']="text/css ";
  107. $this- > xlq_filetype['hqx']="application/mac-binhex40";
  108. $this->xlq_filetype['bin']="application/octet-stream";
  109. $this->xlq_filetype['oda' ] ="application/oda";
  110. $this->xlq_filetype['pdf']="application/pdf";
  111. $this->xlq_filetype['ai']="application/postsrcipt";
  112. $this->xlq_filetype['eps']="application/postsrcipt";
  113. $this->xlq_filetype['es']="application/ postsrcipt";
  114. $this->xlq_filetype['rtf' ] ="application/rtf";
  115. $this->xlq_filetype['mif']="application/x-mif";
  116. $this->xlq_filetype['csh']="application/x-csh " ;
  117. $this->xlq_filetype['dvi']="application/x-dvi";
  118. $this-> ;xlq_filetype['hdf']="application/x-hdf";
  119. $ this->xlq_filetype['nc']="application/x-netcdf";
  120. $this->xlq_filetype['cdf']="application/x-netcdf";
  121. $this->xlq_filetype[ ' latex']="application/x-latex";
  122. $this->xlq_filetype['ts']= "application/x-troll-ts";
  123. $this->xlq_filetype['src'] = "application/x-wais-source";
  124. $this->xlq_filetype['zip']= "application/zip";
  125. $this->xlq_filetype['bcpio']="application/x- bcpio ";
  126. $this->xlq_filetype['cpio']="application/x-cpio";
  127. $this->xlq_filetype['gtar']="application/x-gtar";
  128. $ this->xlq_filetype['shar']="application/x-shar";
  129. $this->xlq_filetype['sv4cpio']="application/x-sv4cpio";
  130. $this->xlq_filetype[ ' sv4crc']="application/x-sv4crc";
  131. $this->xlq_filetype['tar']="application/x-tar";
  132. $this->q_filetype['xlar']=" application /x-ustar";
  133. $this->xlq_filetype[ 'man']="application/x-troff-man";
  134. $this->xlq_filetype['sh']="application/x- sh ";
  135. $this->xlq_filetype['tcl']="application/x-tcl";
  136. $this->xlq_filetype['tex']="application/x-tex";
  137. $ this->xlq_filetype['texi']="application/x-texinfo";
  138. $this->xlq_filetype['texinfo']="application/x-texinfo";
  139. $this->xlq_filetype[ ' t']="application/x-troff";
  140. $this->xlq_filetype['tr']="application/x-troff";
  141. $this->xlq_filetype['roff']=" application /x-troff";
  142. $this->xlq_filetype['shar']="application/x-shar";
  143. $this->xlq_filetype['me']="application/x-troll-我";
  144. $this->xlq_filetype['ts']="application/x-troll-ts";
  145. $this->xlq_filetype['gif']="image/gif";
  146. $ this-> ;xlq_filetype['jpeg']="image/pjpeg";
  147. $this->xlq_filetype['jpg']="image/pjpeg";
  148. $this->xlq_filetype['jpe' ] = "image/pjpeg";
  149. $this->xlq_filetype['ras']="image/x-cmu-raster";
  150. $this->xlq_filetype['pbm']="image/x - portable-bitmap";
  151. $this->xlq_filetype['ppm']="image/x-portable-pixmap";
  152. $this->xlq_filetype['xbm']="image/x -xbitmap " ;
  153. $this->xlq_filetype['xwd']="image/x-xwindowdump";
  154. $this->xlq_filetype['ief']="image/ief";
  155. $this- > xlq_filetype['tif']="image/tiff";
  156. $this->xlq_filetype['tiff']="image/tiff";
  157. $this->xlq_filetype[ 'pnm']="image / x-portable-anymap";
  158. $this->xlq_filetype['pgm']="image/x-portable-graymap";
  159. $this->xlq_filetype[ 'rgb']="image/x - rgb";
  160. $this->xlq_filetype['xpm']="image/x-xpixmap";
  161. $this->xlq_filetype['txt'] ="text/plain";
  162. $ this ->xlq_filetype['c']="text/plain";
  163. $this->xlq_filetype['cc']="text/plain";
  164. $this->xlq_filetype['h']= " text/plain";
  165. $this->xlq_filetype['html']="text/html";
  166. $this->xlq_filetype ['htm']="text/html";
  167. $ this ->xlq_filetype['htl']="text/html";
  168. $this->xlq_filetype['rtx']="text /richtext";
  169. $this->xlq_filetype['etx']= " text/x-settext";
  170. $this->xlq_filetype['tsv']="text/tab-separated-values ”;
  171. $this->xlq_filetype['mpeg']="video/mpeg";
  172. $this->xlq_filetype['mpg']="video/mpeg";
  173. $this->xlq_filetype['mpe'] ="video/mpeg";
  174. $this->xlq_filetype['avi']="video/x-msvideo";
  175. $this->xlq_filetype['qt']="video /quicktime";
  176. $this->xlq_filetype['mov']="video/quicktime";
  177. $this->xlq_filetype['moov']="video/quicktime";
  178. $this->xlq_filetype['movie ']="video/x-sgi-movie";
  179. $this->xlq_filetype['au']="音訊/basic";
  180. $this->xlq_filetype['snd']="音訊/基本";
  181. $this->xlq_filetype['wav']="audio/x-wav";
  182. $this->xlq_filetype['aif'] ="audio/x-aiff";
  183. $this->xlq_filetype['aiff']="audio/x-aiff";
  184. $this->xlq_filetype['aifc']="audio/x-aiff";
  185. $this->xlq_filetype[ 'swf']="application/x-shockwave-flash";
  186. }
  187. }
  188. ?>
複製程式碼
複製程式碼


複製程式碼
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板