Download.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace app\common\util;
  3. class Download
  4. {
  5. var $filepath;
  6. var $downname;
  7. var $ErrInfo;
  8. var $is_attachment = false;
  9. var $_LANG = array(
  10. 'err' => '错误',
  11. 'args_empty' => '参数错误。',
  12. 'file_not_exists' => '文件不存在!',
  13. 'file_not_readable' => '文件不可读!',
  14. );
  15. var $MIMETypes = array(
  16. 'ez' => 'application/andrew-inset',
  17. 'hqx' => 'application/mac-binhex40',
  18. 'cpt' => 'application/mac-compactpro',
  19. 'doc' => 'application/msword',
  20. 'bin' => 'application/octet-stream',
  21. 'dms' => 'application/octet-stream',
  22. 'lha' => 'application/octet-stream',
  23. 'lzh' => 'application/octet-stream',
  24. 'exe' => 'application/octet-stream',
  25. 'class' => 'application/octet-stream',
  26. 'so' => 'application/octet-stream',
  27. 'dll' => 'application/octet-stream',
  28. 'oda' => 'application/oda',
  29. 'pdf' => 'application/pdf',
  30. 'ai' => 'application/postscrīpt',
  31. 'eps' => 'application/postscrīpt',
  32. 'ps' => 'application/postscrīpt',
  33. 'smi' => 'application/smil',
  34. 'smil' => 'application/smil',
  35. 'mif' => 'application/vnd.mif',
  36. 'xls' => 'application/vnd.ms-excel',
  37. 'ppt' => 'application/vnd.ms-powerpoint',
  38. 'wbxml' => 'application/vnd.wap.wbxml',
  39. 'wmlc' => 'application/vnd.wap.wmlc',
  40. 'wmlsc' => 'application/vnd.wap.wmlscrīptc',
  41. 'bcpio' => 'application/x-bcpio',
  42. 'vcd' => 'application/x-cdlink',
  43. 'pgn' => 'application/x-chess-pgn',
  44. 'cpio' => 'application/x-cpio',
  45. 'csh' => 'application/x-csh',
  46. 'dcr' => 'application/x-director',
  47. 'dir' => 'application/x-director',
  48. 'dxr' => 'application/x-director',
  49. 'dvi' => 'application/x-dvi',
  50. 'spl' => 'application/x-futuresplash',
  51. 'gtar' => 'application/x-gtar',
  52. 'hdf' => 'application/x-hdf',
  53. 'js' => 'application/x-javascrīpt',
  54. 'skp' => 'application/x-koan',
  55. 'skd' => 'application/x-koan',
  56. 'skt' => 'application/x-koan',
  57. 'skm' => 'application/x-koan',
  58. 'latex' => 'application/x-latex',
  59. 'nc' => 'application/x-netcdf',
  60. 'cdf' => 'application/x-netcdf',
  61. 'sh' => 'application/x-sh',
  62. 'shar' => 'application/x-shar',
  63. 'swf' => 'application/x-shockwave-flash',
  64. 'sit' => 'application/x-stuffit',
  65. 'sv4cpio' => 'application/x-sv4cpio',
  66. 'sv4crc' => 'application/x-sv4crc',
  67. 'tar' => 'application/x-tar',
  68. 'tcl' => 'application/x-tcl',
  69. 'tex' => 'application/x-tex',
  70. 'texinfo' => 'application/x-texinfo',
  71. 'texi' => 'application/x-texinfo',
  72. 't' => 'application/x-troff',
  73. 'tr' => 'application/x-troff',
  74. 'roff' => 'application/x-troff',
  75. 'man' => 'application/x-troff-man',
  76. 'me' => 'application/x-troff-me',
  77. 'ms' => 'application/x-troff-ms',
  78. 'ustar' => 'application/x-ustar',
  79. 'src' => 'application/x-wais-source',
  80. 'xhtml' => 'application/xhtml+xml',
  81. 'xht' => 'application/xhtml+xml',
  82. 'zip' => 'application/zip',
  83. 'au' => 'audio/basic',
  84. 'snd' => 'audio/basic',
  85. 'mid' => 'audio/midi',
  86. 'midi' => 'audio/midi',
  87. 'kar' => 'audio/midi',
  88. 'mpga' => 'audio/mpeg',
  89. 'mp2' => 'audio/mpeg',
  90. 'mp3' => 'audio/mpeg',
  91. 'wma' => 'audio/mpeg',
  92. 'aif' => 'audio/x-aiff',
  93. 'aiff' => 'audio/x-aiff',
  94. 'aifc' => 'audio/x-aiff',
  95. 'm3u' => 'audio/x-mpegurl',
  96. 'ram' => 'audio/x-pn-realaudio',
  97. 'rm' => 'audio/x-pn-realaudio',
  98. 'rpm' => 'audio/x-pn-realaudio-plugin',
  99. 'ra' => 'audio/x-realaudio',
  100. 'wav' => 'audio/x-wav',
  101. 'pdb' => 'chemical/x-pdb',
  102. 'xyz' => 'chemical/x-xyz',
  103. 'bmp' => 'image/bmp',
  104. 'gif' => 'image/gif',
  105. 'ief' => 'image/ief',
  106. 'jpeg' => 'image/jpeg',
  107. 'jpg' => 'image/jpeg',
  108. 'jpe' => 'image/jpeg',
  109. 'png' => 'image/png',
  110. 'tiff' => 'image/tiff',
  111. 'tif' => 'image/tiff',
  112. 'djvu' => 'image/vnd.djvu',
  113. 'djv' => 'image/vnd.djvu',
  114. 'wbmp' => 'image/vnd.wap.wbmp',
  115. 'ras' => 'image/x-cmu-raster',
  116. 'pnm' => 'image/x-portable-anymap',
  117. 'pbm' => 'image/x-portable-bitmap',
  118. 'pgm' => 'image/x-portable-graymap',
  119. 'ppm' => 'image/x-portable-pixmap',
  120. 'rgb' => 'image/x-rgb',
  121. 'xbm' => 'image/x-xbitmap',
  122. 'xpm' => 'image/x-xpixmap',
  123. 'xwd' => 'image/x-xwindowdump',
  124. 'igs' => 'model/iges',
  125. 'iges' => 'model/iges',
  126. 'msh' => 'model/mesh',
  127. 'mesh' => 'model/mesh',
  128. 'silo' => 'model/mesh',
  129. 'wrl' => 'model/vrml',
  130. 'vrml' => 'model/vrml',
  131. 'css' => 'text/css',
  132. 'html' => 'text/html',
  133. 'htm' => 'text/html',
  134. 'asc' => 'text/plain',
  135. 'txt' => 'text/plain',
  136. 'rtx' => 'text/richtext',
  137. 'rtf' => 'text/rtf',
  138. 'sgml' => 'text/sgml',
  139. 'sgm' => 'text/sgml',
  140. 'tsv' => 'text/tab-separated-values',
  141. 'wml' => 'text/vnd.wap.wml',
  142. 'wmls' => 'text/vnd.wap.wmlscrīpt',
  143. 'etx' => 'text/x-setext',
  144. 'xsl' => 'text/xml',
  145. 'xml' => 'text/xml',
  146. 'mpeg' => 'video/mpeg',
  147. 'mpg' => 'video/mpeg',
  148. 'mpe' => 'video/mpeg',
  149. 'qt' => 'video/quicktime',
  150. 'mov' => 'video/quicktime',
  151. 'mxu' => 'video/vnd.mpegurl',
  152. 'avi' => 'video/x-msvideo',
  153. 'movie' => 'video/x-sgi-movie',
  154. 'wmv' => 'application/x-mplayer2',
  155. 'ice' => 'x-conference/x-cooltalk',
  156. );
  157. function Download($filepath='',$downname='')
  158. {
  159. if($filepath == '' AND !$this->filepath)
  160. {
  161. $this->ErrInfo = $this->_LANG['err'] . ':' . $this->_LANG['args_empty'];
  162. return false;
  163. }
  164. if($filepath == '') $filepath = $this->filepath;
  165. if(!file_exists($filepath))
  166. {
  167. $this->ErrInfo = $this->_LANG['err'] . ':' . $this->_LANG['file_not_exists'];
  168. return false;
  169. }
  170. if($downname == '' AND !$this->downname) $downname = $filepath;
  171. if($downname == '') $downname = $this->downname;
  172. // 文件扩展名
  173. $fileExt = substr(strrchr($filepath, '.'), 1);
  174. // 文件类型
  175. $fileType = $this->MIMETypes[$fileExt] ? $this->MIMETypes[$fileExt] : 'application/octet-stream';
  176. // 是否是图片
  177. $isImage = False;
  178. /*
  179. 简述: getimagesize(), 详见手册
  180. 说明: 判定某个文件是否为图片的有效手段, 常用在文件上传验证
  181. */
  182. $imgInfo = @getimagesize($filepath);
  183. if ($imgInfo[2] && $imgInfo['bits'])
  184. {
  185. $fileType = $imgInfo['mime']; // 支持不标准扩展名
  186. $isImage = True;
  187. }
  188. // 显示方式
  189. if($this->is_attachment)
  190. {
  191. $attachment = 'attachment'; // 指定弹出下载对话框
  192. }
  193. else
  194. {
  195. $attachment = $isImage ? 'inline' : 'attachment';
  196. }
  197. // 读取文件
  198. if (is_readable($filepath))
  199. {
  200. /*
  201. 简述: ob_end_clean() 清空并关闭输出缓冲, 详见手册
  202. 说明: 关闭输出缓冲, 使文件片段内容读取至内存后即被送出, 减少资源消耗
  203. */
  204. ob_end_clean();
  205. /*
  206. HTTP头信息: 指示客户机可以接收生存期不大于指定时间(秒)的响应
  207. */
  208. header('Cache-control: max-age=31536000');
  209. /*
  210. HTTP头信息: 缓存文件过期时间(格林威治标准时)
  211. */
  212. header('Expires: ' . gmdate('D, d M Y H:i:s', time()+31536000) . ' GMT');
  213. /*
  214. HTTP头信息: 文件在服务期端最后被修改的时间
  215. Cache-control,Expires,Last-Modified 都是控制浏览器缓存的头信息
  216. 在一些访问量巨大的门户, 合理的设置缓存能够避免过多的服务器请求, 一定程度下缓解服务器的压力
  217. */
  218. // header('Last-Modified: ' . gmdate('D, d M Y H:i:s' , filemtime($filepath) . ' GMT'));
  219. /*
  220. HTTP头信息: 文档的编码(Encode)方法, 因为附件请求的文件多样化, 改变编码方式有可能损坏文件, 故为none
  221. */
  222. header('Content-Encoding: none');
  223. /*
  224. HTTP头信息: 告诉浏览器当前请求的文件类型.
  225. 1.始终指定为application/octet-stream, 就代表文件是二进制流, 始终提示下载.
  226. 2.指定对应的类型, 如请求的是mp3文件, 对应的MIME类型是audio/mpeg, IE就会自动启动Windows Media Player进行播放.
  227. */
  228. header('Content-type: ' . $fileType);
  229. /*
  230. HTTP头信息: 如果为attachment, 则告诉浏览器, 在访问时弹出”文件下载”对话框, 并指定保存时文件的默认名称(可以与服务器的文件名不同)
  231. 如果要让浏览器直接显示内容, 则要指定为inline, 如图片, 文本
  232. */
  233. header('Content-Disposition: ' . $attachment . '; filename=' . $downname);
  234. /*
  235. HTTP头信息: 告诉浏览器文件长度
  236. (IE下载文件的时候不是有文件大小信息么?)
  237. */
  238. header('Content-Length: ' . filesize($filepath));
  239. // 打开文件(二进制只读模式)
  240. $fp = fopen($filepath, 'rb');
  241. // 输出文件
  242. fpassthru($fp);
  243. // 关闭文件
  244. fclose($fp);
  245. return true;
  246. }
  247. else
  248. {
  249. $this->error = $this->_LANG['err'] . ':' . $this->_LANG['file_not_readable'];
  250. return false;
  251. }
  252. }
  253. }