ホームページ  >  記事  >  バックエンド開発  >  素敵な PHP ファイル ダウンロード クラス

素敵な PHP ファイル ダウンロード クラス

WBOY
WBOYオリジナル
2016-07-25 08:58:461250ブラウズ
  1. /**

  2. ** PHP ファイル ダウンロード クラス
  3. ** 例:
  4. $download=new download('php,exe,html',false);
  5. if(!$download->down​​loadfile($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. function setFilter($fileFilter)

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

  33. function setfilename($filename)

  34. {
  35. $this->filename=$filename;
  36. }

  37. function 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( "Expires: 0" ); // 有効期限を設定します
  45. header( "Cache-Component: must-revalidate, post-check=0, pre-check=0" );
  46. header( "Content-type:".$this->mineType );
  47. header( "Content-Length: " . filesize( $this->filename ) );
  48. header( "Content-Disposition:attachment; filename="$fn"" );
  49. header( 'Content-Transfer-Encoding: binary ' );
  50. readfile( $this->filename );
  51. return true;
  52. }else
  53. {
  54. return false;
  55. }
  56. }
  57. function geterrormsg()
  58. {
  59. return $this->errormsg;
  60. }< ;/p>
  61. function filecheck()

  62. {
  63. $filename=$this->filename;
  64. if(file_exists($filename))
  65. {
  66. $filetype=strto lower(array_pop(explode('.' ,$filename)));
  67. if(in_array($filetype,$this->Filter))
  68. {
  69. $this->errormsg.=$filename.'不允许下ダウンロード!';
  70. if($this- >debug) exit($filename.'不允许ダウンロード!') ;
  71. return false;
  72. }else
  73. {
  74. if ( function_exists( "mime_content_type" ) )
  75. {
  76. $this->mineType = mime_content_type( $filename );
  77. }
  78. if(empty($this-> MineType))
  79. {
  80. if( isset($this->xlq_filetype[$filetype]) ) $this->mineType = $this->xlq_filetype[$filetype];
  81. }
  82. if(!empty($this) ->mineType))
  83. return true;
  84. else
  85. {
  86. $this->errormsg.='「.$filename.' ファイル タイプの取得中にエラーが発生したか、所定のファイル タイプが存在しません」;
  87. if($ this->debug) exit('ファイル タイプの取得エラー');
  88. return false;
  89. }
  90. }
  91. }else
  92. {
  93. $this->errormsg.=$filename.'存在しません!' ;
  94. if( $this->debug) exit($filename.'存在しません!') ;
  95. return false;
  96. }
  97. }

  98. function setfiletype()

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

复制代


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。