Home > Backend Development > PHP Tutorial > A nice PHP file download class

A nice PHP file download class

WBOY
Release: 2016-07-25 08:58:46
Original
1301 people have browsed it
  1. /**

  2. ** PHP file download class
  3. ** Example:
  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. class download{
  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. function download($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(',',strtolower($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" ); // set expiration time
  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. }

  61. function filecheck()

  62. {
  63. $filename=$this->filename;
  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. 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.='An error occurred while obtaining the '.$filename.' file type, or the predetermined file type does not exist';
  87. if($ this->debug) exit('Error getting file type');
  88. return false;
  89. }
  90. }
  91. }else
  92. {
  93. $this->errormsg.=$filename.'Does not exist!';
  94. if( $this->debug) exit($filename.'Does not exist!') ;
  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']='application/octet-stream';
  105. $this->xlq_filetype['rar']='application/octet-stream';
  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']="application/pdf";
  112. $this->xlq_filetype['ai']="application/postsrcipt";
  113. $this->xlq_filetype['eps']="application/postsrcipt";
  114. $this->xlq_filetype['es']="application/postsrcipt";
  115. $this->xlq_filetype['rtf']="application/rtf";
  116. $this->xlq_filetype['mif']="application/x-mif";
  117. $this->xlq_filetype['csh']="application/x-csh";
  118. $this->xlq_filetype['dvi']="application/x-dvi";
  119. $this->xlq_filetype['hdf']="application/x-hdf";
  120. $this->xlq_filetype['nc']="application/x-netcdf";
  121. $this->xlq_filetype['cdf']="application/x-netcdf";
  122. $this->xlq_filetype['latex']="application/x-latex";
  123. $this->xlq_filetype['ts']="application/x-troll-ts";
  124. $this->xlq_filetype['src']="application/x-wais-source";
  125. $this->xlq_filetype['zip']="application/zip";
  126. $this->xlq_filetype['bcpio']="application/x-bcpio";
  127. $this->xlq_filetype['cpio']="application/x-cpio";
  128. $this->xlq_filetype['gtar']="application/x-gtar";
  129. $this->xlq_filetype['shar']="application/x-shar";
  130. $this->xlq_filetype['sv4cpio']="application/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']="application/x-tex";
  138. $this->xlq_filetype['texi']="application/x-texinfo";
  139. $this->xlq_filetype['texinfo']="application/x-texinfo";
  140. $this->xlq_filetype['t']="application/x-troff";
  141. $this->xlq_filetype['tr']="application/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-me";
  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']="text/richtext";
  170. $this->xlq_filetype['etx']="text/x-setext";
  171. $this->xlq_filetype['tsv']="text/tab-separated-values";
  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']="video/quicktime";
  178. $this->xlq_filetype['moov']="video/quicktime";
  179. $this->xlq_filetype['movie']="video/x-sgi-movie";
  180. $this->xlq_filetype['au']="audio/basic";
  181. $this->xlq_filetype['snd']="audio/basic";
  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. ?>

复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template