Java代码  
  1. <?php
  2. /**
  3. $filename = 'index.php';
  4. $download = new Download('php,exe,html', false);
  5. if (!$download->downloadfile($filename)) {
  6. echo $download->getErrorMsgs();
  7. }
  8. */
  9. class Download {
  10. private $debug = false;
  11. private $errorMsg = '';
  12. private $filter = array();
  13. private $fileName = '';
  14. private $mineType = '';
  15. private $xlq_filetype = array();
  16. private $limitTime = 60;
  17. /**
  18. * @param string $fileFilter
  19. * @param boolean $isDebug
  20. */
  21. function __construct($fileFilter = '', $isDebug = true) {
  22. $this->setFilter($fileFilter);
  23. $this->setDebug($isDebug);
  24. $this->setFileType();
  25. }
  26. function downloadfile($filename) {
  27. $this->fileName = $filename;
  28. if ($this->filecheck()) {
  29. $fn = basename($this->fileName);
  30. ob_end_clean();
  31. @set_time_limit($this->limitTime);
  32. header('Cache-control: max-age=31536000');
  33. header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 31536000) . ' GMT');
  34. header('Content-Encoding: none');
  35. header('Content-Length: ' . filesize($this->fileName));
  36. header('Content-Disposition: attachment; filename=' . $fn);
  37. header('Content-Type: ' . $this->mineType);
  38. readfile($this->fileName);
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. }
  44. function filecheck() {
  45. $fileName = $this->fileName;
  46. if (file_exists($fileName)) {
  47. $fileType = strtolower(array_pop(explode(".", $fileName)));
  48. if (!in_array($fileType, $this->filter)) {
  49. $this->errorMsg .= sprintf("%s 不允许下载", $fileName);
  50. if ($this->debug) exit(sprintf("%s 不允许下载", $fileName));
  51. return false;
  52. } else {
  53. if (function_exists("mime_content_type")) {
  54. $this->mineType = mime_content_type($fileName);
  55. }
  56. if (empty($this->mineType)) {
  57. if (isset($this->xlq_filetype[$fileType])) {
  58. $this->mineType = $this->xlq_filetype[$fileType];
  59. }
  60. }
  61. if (!empty($this->mineType)) {
  62. return true;
  63. } else {
  64. $this->errorMsg .= "获取文件类型出错";
  65. if ($this->debug) exit("获取文件类型出错");
  66. return false;
  67. }
  68. }
  69. } else {
  70. $this->errorMsg .= sprintf("%s 不存在", $fileName);
  71. if ($this->debug) exit(sprintf("%s 不存在", $fileName));
  72. return false;
  73. }
  74. }
  75. function setFileType() {
  76. $this->xlq_filetype['chm'] = 'application/octet-stream';
  77. $this->xlq_filetype['ppt'] = 'application/vnd.ms-powerpoint';
  78. $this->xlq_filetype['xls'] = 'application/vnd.ms-excel';
  79. $this->xlq_filetype['doc'] = 'application/msword';
  80. $this->xlq_filetype['exe'] = 'application/octet-stream';
  81. $this->xlq_filetype['rar'] = 'application/octet-stream';
  82. $this->xlq_filetype['js'] = "javascript/js";
  83. $this->xlq_filetype['css'] = "text/css";
  84. $this->xlq_filetype['hqx'] = "application/mac-binhex40";
  85. $this->xlq_filetype['bin'] = "application/octet-stream";
  86. $this->xlq_filetype['oda'] = "application/oda";
  87. $this->xlq_filetype['pdf'] = "application/pdf";
  88. $this->xlq_filetype['ai'] = "application/postsrcipt";
  89. $this->xlq_filetype['eps'] = "application/postsrcipt";
  90. $this->xlq_filetype['es'] = "application/postsrcipt";
  91. $this->xlq_filetype['rtf'] = "application/rtf";
  92. $this->xlq_filetype['mif'] = "application/x-mif";
  93. $this->xlq_filetype['csh'] = "application/x-csh";
  94. $this->xlq_filetype['dvi'] = "application/x-dvi";
  95. $this->xlq_filetype['hdf'] = "application/x-hdf";
  96. $this->xlq_filetype['nc'] = "application/x-netcdf";
  97. $this->xlq_filetype['cdf'] = "application/x-netcdf";
  98. $this->xlq_filetype['latex'] = "application/x-latex";
  99. $this->xlq_filetype['ts'] = "application/x-troll-ts";
  100. $this->xlq_filetype['src'] = "application/x-wais-source";
  101. $this->xlq_filetype['zip'] = "application/zip";
  102. $this->xlq_filetype['bcpio'] = "application/x-bcpio";
  103. $this->xlq_filetype['cpio'] = "application/x-cpio";
  104. $this->xlq_filetype['gtar'] = "application/x-gtar";
  105. $this->xlq_filetype['shar'] = "application/x-shar";
  106. $this->xlq_filetype['sv4cpio'] = "application/x-sv4cpio";
  107. $this->xlq_filetype['sv4crc'] = "application/x-sv4crc";
  108. $this->xlq_filetype['tar'] = "application/x-tar";
  109. $this->xlq_filetype['ustar'] = "application/x-ustar";
  110. $this->xlq_filetype['man'] = "application/x-troff-man";
  111. $this->xlq_filetype['sh'] = "application/x-sh";
  112. $this->xlq_filetype['tcl'] = "application/x-tcl";
  113. $this->xlq_filetype['tex'] = "application/x-tex";
  114. $this->xlq_filetype['texi'] = "application/x-texinfo";
  115. $this->xlq_filetype['texinfo'] = "application/x-texinfo";
  116. $this->xlq_filetype['t'] = "application/x-troff";
  117. $this->xlq_filetype['tr'] = "application/x-troff";
  118. $this->xlq_filetype['roff'] = "application/x-troff";
  119. $this->xlq_filetype['shar'] = "application/x-shar";
  120. $this->xlq_filetype['me'] = "application/x-troll-me";
  121. $this->xlq_filetype['ts'] = "application/x-troll-ts";
  122. $this->xlq_filetype['gif'] = "image/gif";
  123. $this->xlq_filetype['jpeg'] = "image/pjpeg";
  124. $this->xlq_filetype['jpg'] = "image/pjpeg";
  125. $this->xlq_filetype['jpe'] = "image/pjpeg";
  126. $this->xlq_filetype['ras'] = "image/x-cmu-raster";
  127. $this->xlq_filetype['pbm'] = "image/x-portable-bitmap";
  128. $this->xlq_filetype['ppm'] = "image/x-portable-pixmap";
  129. $this->xlq_filetype['xbm'] = "image/x-xbitmap";
  130. $this->xlq_filetype['xwd'] = "image/x-xwindowdump";
  131. $this->xlq_filetype['ief'] = "image/ief";
  132. $this->xlq_filetype['tif'] = "image/tiff";
  133. $this->xlq_filetype['tiff'] = "image/tiff";
  134. $this->xlq_filetype['pnm'] = "image/x-portable-anymap";
  135. $this->xlq_filetype['pgm'] = "image/x-portable-graymap";
  136. $this->xlq_filetype['rgb'] = "image/x-rgb";
  137. $this->xlq_filetype['xpm'] = "image/x-xpixmap";
  138. $this->xlq_filetype['txt'] = "text/plain";
  139. $this->xlq_filetype['c'] = "text/plain";
  140. $this->xlq_filetype['cc'] = "text/plain";
  141. $this->xlq_filetype['h'] = "text/plain";
  142. $this->xlq_filetype['html'] = "text/html";
  143. $this->xlq_filetype['htm'] = "text/html";
  144. $this->xlq_filetype['htl'] = "text/html";
  145. $this->xlq_filetype['rtx'] = "text/richtext";
  146. $this->xlq_filetype['etx'] = "text/x-setext";
  147. $this->xlq_filetype['tsv'] = "text/tab-separated-values";
  148. $this->xlq_filetype['mpeg'] = "video/mpeg";
  149. $this->xlq_filetype['mpg'] = "video/mpeg";
  150. $this->xlq_filetype['mpe'] = "video/mpeg";
  151. $this->xlq_filetype['avi'] = "video/x-msvideo";
  152. $this->xlq_filetype['qt'] = "video/quicktime";
  153. $this->xlq_filetype['mov'] = "video/quicktime";
  154. $this->xlq_filetype['moov'] = "video/quicktime";
  155. $this->xlq_filetype['movie'] = "video/x-sgi-movie";
  156. $this->xlq_filetype['au'] = "audio/basic";
  157. $this->xlq_filetype['snd'] = "audio/basic";
  158. $this->xlq_filetype['wav'] = "audio/x-wav";
  159. $this->xlq_filetype['aif'] = "audio/x-aiff";
  160. $this->xlq_filetype['aiff'] = "audio/x-aiff";
  161. $this->xlq_filetype['aifc'] = "audio/x-aiff";
  162. $this->xlq_filetype['swf'] = "application/x-shockwave-flash";
  163. }
  164. function setFilter($fileFilter) {
  165. if (empty($fileFilter)) return;
  166. $this->filter = explode(",", strtolower($fileFilter));
  167. }
  168. function setDebug($debug) {
  169. $this->debug = $debug;
  170. }
  171. function setlimittime($limittime) {
  172. $this->limitTime = $limittime;
  173. }
  174. function getfilename($filename) {
  175. return $this->fileName;
  176. }
  177. function getErrorMsgs() {
  178. return $this->errorMsg;
  179. }
  180. function __destruct() {
  181. $this->errorMsg = '';
  182. }
  183. }
  184. ?>

PHP文件下载过滤类相关推荐

  1. java downloadfile_FileDownload.java:文件下载工具类

    FileDownload.java:文件下载工具类 2017-04-25·Mr.Xia 4489 次浏览 文件下载功能的工具类 JavaWeb工具类目录 [http://baike.xsoftlab. ...

  2. vb一个使用URLDownloadToFile实现文件下载的类

    一个使用URLDownloadToFile函数和IBindStatusCallback接口编写的文件下载类,类名称为URLDownFile.在复制以下代码之前,朋友们需要先先下载Edanmo编写的一个 ...

  3. 推荐系统-模型(一):召回模型【协同过滤类: ItemCF/UserCF】【Embedding类】【Dssm/双塔/word2vec】【图类召回算法 (Deepwalk、EGES)】

    推荐系统-召回模型:[协同过滤类: ItemCF/UserCF][Embedding类][Dssm/双塔/word2vec][图类召回算法 (Deepwalk.EGES)]

  4. Java文件下载工具类

    各种文件下载 /** 文件下载工具类 */public void downloadFile(HttpServletResponse response,String url)throws Unsuppo ...

  5. 某游戏研究之字符过滤类-WorldFilter

    所谓字符过滤器,常常用在聊天的内容,比如一连串的骂人难听的话,我们要屏蔽掉,避免造成不好的东西! 当然我作为中华天朝一个有文明有素质的人,肯定偶尔会做这样的事情啦,特别是打LOL的时候,算了不讲了,都 ...

  6. Android基于Okhttp3的文件下载工具类

    需求中有需要简易的下载文件的,例如图片,音频,视频等.首先这个下载工具类没有断点下载,也就是说没有暂停,缓存.不过解决日常工作中的小文件下载是绰绰有余的.下面可以看一看 一.添加okhttp3的远程依 ...

  7. php 严格过滤,php 常用过滤类

    //$str = '汉hah子abc---ABC123_-'; // if(!preg_match("/^[".chr(0xa1)."-".chr(0xff). ...

  8. java 下载工具_java文件下载工具类

    java文件工具类是java开发爱好者提供的一个java开源包,让广大java爱好者以及开发者可以直接编译形成class文件进行使用.内容包含io流,图形界面,使用户更好更快的找到相对应文件,并进行相 ...

  9. java工具类下载_java文件下载工具类

    /** * Desc:下载工具类 * @date 2014-6-26 */ public class DownLoadUtil { /** * Desc:根据文件头判断请求来自的浏览器,以便有针对性的 ...

最新文章

  1. java numberutil_NumberUtil
  2. 【NLP】关于Transformer的常见问题及解答
  3. 【机器视觉】 gen_measure_arc算子
  4. sql like 绑定变量_码硬解析的改进方法之二使用绑定变量
  5. 15个Linux Yum命令实例--安装/卸载/更新
  6. 最擅长玩《毁灭战士》的AI开源了 | 来自CMU的论文代码
  7. java 回溯_java 实现迷宫回溯算法示例详解
  8. linux ip onboot,linux onboot no
  9. Linux-目录和文件管理(二)
  10. 历年计算机二级Java考试试题及答案(完整版)
  11. linux如何部署btsync同步?
  12. 天轰穿&甜老丝儿 python浅显易懂全教程
  13. python中compile函数_Python compile函数有什么用?
  14. 层叠上下文、层叠等级、层叠顺序
  15. 使用Transformer学习动态多层次的交通时空特征
  16. matlab报错Unbalanced or unexpected parenthesis or bracket.
  17. Python编写的com组件大全与解决对策
  18. 开关电源LLC谐振变换器的工作原理
  19. 一周疫情对行业影响观察 | 中国资产或成避险资产;亚太旅游零售业呼吁政府财政支持...
  20. 4480: [Jsoi2013]快乐的jyy

热门文章

  1. 70进货卖100利润是多少_一只周黑鸭随便就卖100多,那成本有多少?说出来你可能不信...
  2. 仲裁文件夹服务器,仲裁服务器是什么
  3. 杭电2005题c语言答案,杭电2005
  4. 2014职称计算机考试光盘,江苏省职称计算机考试光盘答案.doc
  5. jitter 如何优化网络_抖音优化该怎么做?
  6. w3c html5 客户端缓存数据格式,Html5应用程序缓存(Cache manifest)
  7. 怎么做95置信区间图_这种动态的OD图怎么做?简单3步快速搞定
  8. python装饰器带参数函数二阶导数公式_一文搞定Python装饰器,看完面试不再慌
  9. centos7源码安装mysql报错_CentOS7 下源码安装MySQL数据库 8.0.11
  10. java 泛型 加_Java泛型并将数字加在一起