PHP文件下载过滤类

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值