PHP文件下载过滤类
![](http://hudeyong926.iteye.com/images/icon_star.png)
- <?php
- /**
- $filename = 'index.php';
- $download = new Download('php,exe,html', false);
- if (!$download->downloadfile($filename)) {
- echo $download->getErrorMsgs();
- }
- */
- class Download {
- private $debug = false;
- private $errorMsg = '';
- private $filter = array();
- private $fileName = '';
- private $mineType = '';
- private $xlq_filetype = array();
- private $limitTime = 60;
- /**
- * @param string $fileFilter
- * @param boolean $isDebug
- */
- function __construct($fileFilter = '', $isDebug = true) {
- $this->setFilter($fileFilter);
- $this->setDebug($isDebug);
- $this->setFileType();
- }
- function downloadfile($filename) {
- $this->fileName = $filename;
- if ($this->filecheck()) {
- $fn = basename($this->fileName);
- ob_end_clean();
- @set_time_limit($this->limitTime);
- header('Cache-control: max-age=31536000');
- header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 31536000) . ' GMT');
- header('Content-Encoding: none');
- header('Content-Length: ' . filesize($this->fileName));
- header('Content-Disposition: attachment; filename=' . $fn);
- header('Content-Type: ' . $this->mineType);
- readfile($this->fileName);
- return true;
- } else {
- return false;
- }
- }
- function filecheck() {
- $fileName = $this->fileName;
- if (file_exists($fileName)) {
- $fileType = strtolower(array_pop(explode(".", $fileName)));
- if (!in_array($fileType, $this->filter)) {
- $this->errorMsg .= sprintf("%s 不允许下载", $fileName);
- if ($this->debug) exit(sprintf("%s 不允许下载", $fileName));
- return false;
- } else {
- if (function_exists("mime_content_type")) {
- $this->mineType = mime_content_type($fileName);
- }
- if (empty($this->mineType)) {
- if (isset($this->xlq_filetype[$fileType])) {
- $this->mineType = $this->xlq_filetype[$fileType];
- }
- }
- if (!empty($this->mineType)) {
- return true;
- } else {
- $this->errorMsg .= "获取文件类型出错";
- if ($this->debug) exit("获取文件类型出错");
- return false;
- }
- }
- } else {
- $this->errorMsg .= sprintf("%s 不存在", $fileName);
- if ($this->debug) exit(sprintf("%s 不存在", $fileName));
- return false;
- }
- }
- function setFileType() {
- $this->xlq_filetype['chm'] = 'application/octet-stream';
- $this->xlq_filetype['ppt'] = 'application/vnd.ms-powerpoint';
- $this->xlq_filetype['xls'] = 'application/vnd.ms-excel';
- $this->xlq_filetype['doc'] = 'application/msword';
- $this->xlq_filetype['exe'] = 'application/octet-stream';
- $this->xlq_filetype['rar'] = 'application/octet-stream';
- $this->xlq_filetype['js'] = "javascript/js";
- $this->xlq_filetype['css'] = "text/css";
- $this->xlq_filetype['hqx'] = "application/mac-binhex40";
- $this->xlq_filetype['bin'] = "application/octet-stream";
- $this->xlq_filetype['oda'] = "application/oda";
- $this->xlq_filetype['pdf'] = "application/pdf";
- $this->xlq_filetype['ai'] = "application/postsrcipt";
- $this->xlq_filetype['eps'] = "application/postsrcipt";
- $this->xlq_filetype['es'] = "application/postsrcipt";
- $this->xlq_filetype['rtf'] = "application/rtf";
- $this->xlq_filetype['mif'] = "application/x-mif";
- $this->xlq_filetype['csh'] = "application/x-csh";
- $this->xlq_filetype['dvi'] = "application/x-dvi";
- $this->xlq_filetype['hdf'] = "application/x-hdf";
- $this->xlq_filetype['nc'] = "application/x-netcdf";
- $this->xlq_filetype['cdf'] = "application/x-netcdf";
- $this->xlq_filetype['latex'] = "application/x-latex";
- $this->xlq_filetype['ts'] = "application/x-troll-ts";
- $this->xlq_filetype['src'] = "application/x-wais-source";
- $this->xlq_filetype['zip'] = "application/zip";
- $this->xlq_filetype['bcpio'] = "application/x-bcpio";
- $this->xlq_filetype['cpio'] = "application/x-cpio";
- $this->xlq_filetype['gtar'] = "application/x-gtar";
- $this->xlq_filetype['shar'] = "application/x-shar";
- $this->xlq_filetype['sv4cpio'] = "application/x-sv4cpio";
- $this->xlq_filetype['sv4crc'] = "application/x-sv4crc";
- $this->xlq_filetype['tar'] = "application/x-tar";
- $this->xlq_filetype['ustar'] = "application/x-ustar";
- $this->xlq_filetype['man'] = "application/x-troff-man";
- $this->xlq_filetype['sh'] = "application/x-sh";
- $this->xlq_filetype['tcl'] = "application/x-tcl";
- $this->xlq_filetype['tex'] = "application/x-tex";
- $this->xlq_filetype['texi'] = "application/x-texinfo";
- $this->xlq_filetype['texinfo'] = "application/x-texinfo";
- $this->xlq_filetype['t'] = "application/x-troff";
- $this->xlq_filetype['tr'] = "application/x-troff";
- $this->xlq_filetype['roff'] = "application/x-troff";
- $this->xlq_filetype['shar'] = "application/x-shar";
- $this->xlq_filetype['me'] = "application/x-troll-me";
- $this->xlq_filetype['ts'] = "application/x-troll-ts";
- $this->xlq_filetype['gif'] = "image/gif";
- $this->xlq_filetype['jpeg'] = "image/pjpeg";
- $this->xlq_filetype['jpg'] = "image/pjpeg";
- $this->xlq_filetype['jpe'] = "image/pjpeg";
- $this->xlq_filetype['ras'] = "image/x-cmu-raster";
- $this->xlq_filetype['pbm'] = "image/x-portable-bitmap";
- $this->xlq_filetype['ppm'] = "image/x-portable-pixmap";
- $this->xlq_filetype['xbm'] = "image/x-xbitmap";
- $this->xlq_filetype['xwd'] = "image/x-xwindowdump";
- $this->xlq_filetype['ief'] = "image/ief";
- $this->xlq_filetype['tif'] = "image/tiff";
- $this->xlq_filetype['tiff'] = "image/tiff";
- $this->xlq_filetype['pnm'] = "image/x-portable-anymap";
- $this->xlq_filetype['pgm'] = "image/x-portable-graymap";
- $this->xlq_filetype['rgb'] = "image/x-rgb";
- $this->xlq_filetype['xpm'] = "image/x-xpixmap";
- $this->xlq_filetype['txt'] = "text/plain";
- $this->xlq_filetype['c'] = "text/plain";
- $this->xlq_filetype['cc'] = "text/plain";
- $this->xlq_filetype['h'] = "text/plain";
- $this->xlq_filetype['html'] = "text/html";
- $this->xlq_filetype['htm'] = "text/html";
- $this->xlq_filetype['htl'] = "text/html";
- $this->xlq_filetype['rtx'] = "text/richtext";
- $this->xlq_filetype['etx'] = "text/x-setext";
- $this->xlq_filetype['tsv'] = "text/tab-separated-values";
- $this->xlq_filetype['mpeg'] = "video/mpeg";
- $this->xlq_filetype['mpg'] = "video/mpeg";
- $this->xlq_filetype['mpe'] = "video/mpeg";
- $this->xlq_filetype['avi'] = "video/x-msvideo";
- $this->xlq_filetype['qt'] = "video/quicktime";
- $this->xlq_filetype['mov'] = "video/quicktime";
- $this->xlq_filetype['moov'] = "video/quicktime";
- $this->xlq_filetype['movie'] = "video/x-sgi-movie";
- $this->xlq_filetype['au'] = "audio/basic";
- $this->xlq_filetype['snd'] = "audio/basic";
- $this->xlq_filetype['wav'] = "audio/x-wav";
- $this->xlq_filetype['aif'] = "audio/x-aiff";
- $this->xlq_filetype['aiff'] = "audio/x-aiff";
- $this->xlq_filetype['aifc'] = "audio/x-aiff";
- $this->xlq_filetype['swf'] = "application/x-shockwave-flash";
- }
- function setFilter($fileFilter) {
- if (empty($fileFilter)) return;
- $this->filter = explode(",", strtolower($fileFilter));
- }
- function setDebug($debug) {
- $this->debug = $debug;
- }
- function setlimittime($limittime) {
- $this->limitTime = $limittime;
- }
- function getfilename($filename) {
- return $this->fileName;
- }
- function getErrorMsgs() {
- return $this->errorMsg;
- }
- function __destruct() {
- $this->errorMsg = '';
- }
- }
- ?>
PHP文件下载过滤类相关推荐
- java downloadfile_FileDownload.java:文件下载工具类
FileDownload.java:文件下载工具类 2017-04-25·Mr.Xia 4489 次浏览 文件下载功能的工具类 JavaWeb工具类目录 [http://baike.xsoftlab. ...
- vb一个使用URLDownloadToFile实现文件下载的类
一个使用URLDownloadToFile函数和IBindStatusCallback接口编写的文件下载类,类名称为URLDownFile.在复制以下代码之前,朋友们需要先先下载Edanmo编写的一个 ...
- 推荐系统-模型(一):召回模型【协同过滤类: ItemCF/UserCF】【Embedding类】【Dssm/双塔/word2vec】【图类召回算法 (Deepwalk、EGES)】
推荐系统-召回模型:[协同过滤类: ItemCF/UserCF][Embedding类][Dssm/双塔/word2vec][图类召回算法 (Deepwalk.EGES)]
- Java文件下载工具类
各种文件下载 /** 文件下载工具类 */public void downloadFile(HttpServletResponse response,String url)throws Unsuppo ...
- 某游戏研究之字符过滤类-WorldFilter
所谓字符过滤器,常常用在聊天的内容,比如一连串的骂人难听的话,我们要屏蔽掉,避免造成不好的东西! 当然我作为中华天朝一个有文明有素质的人,肯定偶尔会做这样的事情啦,特别是打LOL的时候,算了不讲了,都 ...
- Android基于Okhttp3的文件下载工具类
需求中有需要简易的下载文件的,例如图片,音频,视频等.首先这个下载工具类没有断点下载,也就是说没有暂停,缓存.不过解决日常工作中的小文件下载是绰绰有余的.下面可以看一看 一.添加okhttp3的远程依 ...
- php 严格过滤,php 常用过滤类
//$str = '汉hah子abc---ABC123_-'; // if(!preg_match("/^[".chr(0xa1)."-".chr(0xff). ...
- java 下载工具_java文件下载工具类
java文件工具类是java开发爱好者提供的一个java开源包,让广大java爱好者以及开发者可以直接编译形成class文件进行使用.内容包含io流,图形界面,使用户更好更快的找到相对应文件,并进行相 ...
- java工具类下载_java文件下载工具类
/** * Desc:下载工具类 * @date 2014-6-26 */ public class DownLoadUtil { /** * Desc:根据文件头判断请求来自的浏览器,以便有针对性的 ...
最新文章
- java numberutil_NumberUtil
- 【NLP】关于Transformer的常见问题及解答
- 【机器视觉】 gen_measure_arc算子
- sql like 绑定变量_码硬解析的改进方法之二使用绑定变量
- 15个Linux Yum命令实例--安装/卸载/更新
- 最擅长玩《毁灭战士》的AI开源了 | 来自CMU的论文代码
- java 回溯_java 实现迷宫回溯算法示例详解
- linux ip onboot,linux onboot no
- Linux-目录和文件管理(二)
- 历年计算机二级Java考试试题及答案(完整版)
- linux如何部署btsync同步?
- 天轰穿&甜老丝儿 python浅显易懂全教程
- python中compile函数_Python compile函数有什么用?
- 层叠上下文、层叠等级、层叠顺序
- 使用Transformer学习动态多层次的交通时空特征
- matlab报错Unbalanced or unexpected parenthesis or bracket.
- Python编写的com组件大全与解决对策
- 开关电源LLC谐振变换器的工作原理
- 一周疫情对行业影响观察 | 中国资产或成避险资产;亚太旅游零售业呼吁政府财政支持...
- 4480: [Jsoi2013]快乐的jyy
热门文章
- 70进货卖100利润是多少_一只周黑鸭随便就卖100多,那成本有多少?说出来你可能不信...
- 仲裁文件夹服务器,仲裁服务器是什么
- 杭电2005题c语言答案,杭电2005
- 2014职称计算机考试光盘,江苏省职称计算机考试光盘答案.doc
- jitter 如何优化网络_抖音优化该怎么做?
- w3c html5 客户端缓存数据格式,Html5应用程序缓存(Cache manifest)
- 怎么做95置信区间图_这种动态的OD图怎么做?简单3步快速搞定
- python装饰器带参数函数二阶导数公式_一文搞定Python装饰器,看完面试不再慌
- centos7源码安装mysql报错_CentOS7 下源码安装MySQL数据库 8.0.11
- java 泛型 加_Java泛型并将数字加在一起