php文件下载类

  1. <?  
  2. //====================================================  
  3. // FileName:download.class.php  
  4. // Summary: 文件下载类  
  5. //   
  6. // 使用范例:  
  7. // $download = new download('php,exe,html',false);  
  8. // if(!$download->downloadfile($filename))  
  9. // {  
  10. //    echo $download->geterrormsg();  
  11. // }        
  12. //====================================================  
  13. class download  
  14. {  
  15.   var $debug=true;  
  16.   var $errormsg='';  
  17.   var $Filter=array();  
  18.   var $filename='';  
  19.   var $mineType='text/plain';  
  20.   var $xlq_filetype=array();  
  21.    
  22.   function __construct($fileFilter='',$isdebug=true)  
  23.   {  
  24.     $this->setFilter($fileFilter);  
  25.         $this->setdebug($isdebug);          
  26.         $this->setfiletype();  
  27.   }  
  28.     
  29.   function setFilter($fileFilter)  
  30.   {  
  31.     if(emptyempty($fileFilter)) return ;  
  32.         $this->Filter=explode(',',strtolower($fileFilter));  
  33.   }  
  34.   function setdebug($debug)  
  35.   {  
  36.     $this->debug=$debug;  
  37.   }  
  38.     
  39.   function setfilename($filename)  
  40.   {  
  41.     $this->filename=$filename;  
  42.   }  
  43.     
  44.   function downloadfile($filename)  
  45.   {  
  46.     $this->setfilename($filename);  
  47.     if($this->filecheck())  
  48.     {  
  49.         $fn = array_popexplode'/'strtr$this->filename, '\\', '/' ) ) );  
  50.         header( "Pragma: public" );  
  51.         header( "Expires: 0" ); // set expiration time  
  52.         header( "Cache-Component: must-revalidate, post-check=0, pre-check=0" );  
  53.         header( "Content-type:".$this->mineType );  
  54.         header( "Content-Length: " . filesize$this->filename ) );  
  55.         header( "Content-Disposition: attachment; filename=\"$fn\"" );  
  56.         header( 'Content-Transfer-Encoding: binary' );  
  57.         readfile( $this->filename );  
  58.         return true;  
  59.     }  
  60.     else  
  61.     {  
  62.         return false;  
  63.     }  
  64.   }  
  65.   function geterrormsg()  
  66.   {  
  67.     return $this->errormsg;  
  68.   }  
  69.     
  70.   function filecheck()  
  71.   {  
  72.     $filename=$this->filename;  
  73.         if(file_exists($filename))  
  74.         {  
  75.            $filetype=strtolower(array_pop(explode('.',$filename)));  
  76.            if(in_array($filetype,$this->Filter))  
  77.            {  
  78.              $this->errormsg.=$filename.'不允许下载!';  
  79.                  if($this->debug) exit($filename.'不允许下载!') ;  
  80.                  return false;  
  81.            }else  
  82.            {  
  83.              if ( function_exists( "mime_content_type" ) )  
  84.                  {  
  85.            $this->mineType = mime_content_type( $filename );  
  86.          }  
  87.                  if(emptyempty($this->mineType))  
  88.                  {  
  89.                     if( isset($this->xlq_filetype[$filetype]) )  $this->mineType = $this->xlq_filetype[$filetype];  
  90.                  }  
  91.                  if(!emptyempty($this->mineType))  
  92.                    return true;  
  93.                  else  
  94.                  {  
  95.                     $this->errormsg.='获取'.$filename.'文件类型时候发生错误,或者不存在预定文件类型内';  
  96.                         if($this->debug) exit('获取文件类型出错');  
  97.                         return false;  
  98.                  }  
  99.            }   
  100.         }else  
  101.         {  
  102.           $this->errormsg.=$filename.'不存在!';  
  103.           if($this->debug) exit($filename.'不存在!') ;  
  104.           return false;  
  105.         }  
  106.   }  
  107.     
  108.   function setfiletype()  
  109.   {  
  110.     $this->xlq_filetype['chm']='application/octet-stream';  
  111.     $this->xlq_filetype['ppt']='application/vnd.ms-powerpoint';  
  112.     $this->xlq_filetype['xls']='application/vnd.ms-excel';  
  113.     $this->xlq_filetype['doc']='application/msword';  
  114.     $this->xlq_filetype['exe']='application/octet-stream';  
  115.     $this->xlq_filetype['rar']='application/octet-stream';  
  116.     $this->xlq_filetype['js']="javascript/js";  
  117.     $this->xlq_filetype['css']="text/css";  
  118.     $this->xlq_filetype['hqx']="application/mac-binhex40";  
  119.     $this->xlq_filetype['bin']="application/octet-stream";  
  120.     $this->xlq_filetype['oda']="application/oda";  
  121.     $this->xlq_filetype['pdf']="application/pdf";  
  122.     $this->xlq_filetype['ai']="application/postsrcipt";  
  123.     $this->xlq_filetype['eps']="application/postsrcipt";  
  124.     $this->xlq_filetype['es']="application/postsrcipt";  
  125.     $this->xlq_filetype['rtf']="application/rtf";  
  126.     $this->xlq_filetype['mif']="application/x-mif";  
  127.     $this->xlq_filetype['csh']="application/x-csh";  
  128.     $this->xlq_filetype['dvi']="application/x-dvi";  
  129.     $this->xlq_filetype['hdf']="application/x-hdf";  
  130.     $this->xlq_filetype['nc']="application/x-netcdf";  
  131.     $this->xlq_filetype['cdf']="application/x-netcdf";  
  132.     $this->xlq_filetype['latex']="application/x-latex";  
  133.     $this->xlq_filetype['ts']="application/x-troll-ts";  
  134.     $this->xlq_filetype['src']="application/x-wais-source";  
  135.     $this->xlq_filetype['zip']="application/zip";  
  136.     $this->xlq_filetype['bcpio']="application/x-bcpio";  
  137.     $this->xlq_filetype['cpio']="application/x-cpio";  
  138.     $this->xlq_filetype['gtar']="application/x-gtar";  
  139.     $this->xlq_filetype['shar']="application/x-shar";  
  140.     $this->xlq_filetype['sv4cpio']="application/x-sv4cpio";  
  141.     $this->xlq_filetype['sv4crc']="application/x-sv4crc";  
  142.     $this->xlq_filetype['tar']="application/x-tar";  
  143.     $this->xlq_filetype['ustar']="application/x-ustar";  
  144.     $this->xlq_filetype['man']="application/x-troff-man";  
  145.     $this->xlq_filetype['sh']="application/x-sh";  
  146.     $this->xlq_filetype['tcl']="application/x-tcl";  
  147.     $this->xlq_filetype['tex']="application/x-tex";  
  148.     $this->xlq_filetype['texi']="application/x-texinfo";  
  149.     $this->xlq_filetype['texinfo']="application/x-texinfo";  
  150.     $this->xlq_filetype['t']="application/x-troff";  
  151.     $this->xlq_filetype['tr']="application/x-troff";  
  152.     $this->xlq_filetype['roff']="application/x-troff";  
  153.     $this->xlq_filetype['shar']="application/x-shar";  
  154.     $this->xlq_filetype['me']="application/x-troll-me";  
  155.     $this->xlq_filetype['ts']="application/x-troll-ts";  
  156.     $this->xlq_filetype['gif']="image/gif";  
  157.     $this->xlq_filetype['jpeg']="image/pjpeg";  
  158.     $this->xlq_filetype['jpg']="image/pjpeg";  
  159.     $this->xlq_filetype['jpe']="image/pjpeg";  
  160.     $this->xlq_filetype['ras']="image/x-cmu-raster";  
  161.     $this->xlq_filetype['pbm']="image/x-portable-bitmap";  
  162.     $this->xlq_filetype['ppm']="image/x-portable-pixmap";  
  163.     $this->xlq_filetype['xbm']="image/x-xbitmap";  
  164.     $this->xlq_filetype['xwd']="image/x-xwindowdump";  
  165.     $this->xlq_filetype['ief']="image/ief";  
  166.     $this->xlq_filetype['tif']="image/tiff";  
  167.     $this->xlq_filetype['tiff']="image/tiff";  
  168.     $this->xlq_filetype['pnm']="image/x-portable-anymap";  
  169.     $this->xlq_filetype['pgm']="image/x-portable-graymap";  
  170.     $this->xlq_filetype['rgb']="image/x-rgb";  
  171.     $this->xlq_filetype['xpm']="image/x-xpixmap";  
  172.     $this->xlq_filetype['txt']="text/plain";  
  173.     $this->xlq_filetype['c']="text/plain";  
  174.     $this->xlq_filetype['cc']="text/plain";  
  175.     $this->xlq_filetype['h']="text/plain";  
  176.     $this->xlq_filetype['html']="text/html";  
  177.     $this->xlq_filetype['htm']="text/html";  
  178.     $this->xlq_filetype['htl']="text/html";  
  179.     $this->xlq_filetype['rtx']="text/richtext";  
  180.     $this->xlq_filetype['etx']="text/x-setext";  
  181.     $this->xlq_filetype['tsv']="text/tab-separated-values";  
  182.     $this->xlq_filetype['mpeg']="video/mpeg";  
  183.     $this->xlq_filetype['mpg']="video/mpeg";  
  184.     $this->xlq_filetype['mpe']="video/mpeg";  
  185.     $this->xlq_filetype['avi']="video/x-msvideo";  
  186.     $this->xlq_filetype['qt']="video/quicktime";  
  187.     $this->xlq_filetype['mov']="video/quicktime";  
  188.     $this->xlq_filetype['moov']="video/quicktime";  
  189.     $this->xlq_filetype['movie']="video/x-sgi-movie";  
  190.     $this->xlq_filetype['au']="audio/basic";  
  191.     $this->xlq_filetype['snd']="audio/basic";  
  192.     $this->xlq_filetype['wav']="audio/x-wav";  
  193.     $this->xlq_filetype['aif']="audio/x-aiff";  
  194.     $this->xlq_filetype['aiff']="audio/x-aiff";  
  195.     $this->xlq_filetype['aifc']="audio/x-aiff";  
  196.     $this->xlq_filetype['swf']="application/x-shockwave-flash";  
  197.   }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值