PHP文件下载类


Posted in PHP onDecember 06, 2006
<?    
//====================================================    
//   使用范例:    
// $download=new download('php,exe,html',false);    
//  if(!$download->downloadfile($filename))    
//  {    
//    echo $download->geterrormsg();    
//  }          
//====================================================    
class download{    
  var $debug=true;    
  var $errormsg='';    
  var $Filter=array();    
  var $filename='';    
  var $mineType='text/plain';    
  var $xlq_filetype=array();      function download($fileFilter='',$isdebug=true)    
  {    
    $this->setFilter($fileFilter);    
        $this->setdebug($isdebug);            
        $this->setfiletype();    
  }    
  function setFilter($fileFilter)    
  {    
    if(empty($fileFilter)) return ;    
        $this->Filter=explode(',',strtolower($fileFilter));    
  }    
  function setdebug($debug)    
  {    
    $this->debug=$debug;    
  }    
  function setfilename($filename)    
  {    
    $this->filename=$filename;    
  }    
  function downloadfile($filename)    
  {    
    $this->setfilename($filename);    
    if($this->filecheck())    
        {    
          $fn = array_pop( explode( '/', strtr( $this->filename, '\\', '/' ) ) );    
          header( "Pragma: public" );    
      header( "Expires: 0" ); // set expiration time    
      header( "Cache-Component: must-revalidate, post-check=0, pre-check=0" );    
          header( "Content-type:".$this->mineType );    
      header( "Content-Length: " . filesize( $this->filename ) );    
      header( "Content-Disposition: attachment; filename=\"$fn\"" );    
      header( 'Content-Transfer-Encoding: binary' );    
      readfile( $this->filename );    
      return true;    
        }else    
        {    
        return false;    
        }    
  }    
  function geterrormsg()    
  {    
    return $this->errormsg;    
  }    
  function filecheck()    
  {    
    $filename=$this->filename;    
        if(file_exists($filename))    
        {    
           $filetype=strtolower(array_pop(explode('.',$filename)));    
           if(in_array($filetype,$this->Filter))    
           {    
             $this->errormsg.=$filename.'不允许下载!';    
                 if($this->debug) exit($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.='获取'.$filename.'文件类型时候发生错误,或者不存在预定文件类型内';    
                        if($this->debug) exit('获取文件类型出错');    
                        return false;    
                 }    
           }     
        }else    
        {    
          $this->errormsg.=$filename.'不存在!';    
          if($this->debug) exit($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";    
  }    
}    
?> 
PHP 相关文章推荐
php添加文章时生成静态HTML文章的实现代码
Feb 17 PHP
深入理解php的MySQL连接类
Jun 07 PHP
叫你如何修改Nginx与PHP的文件上传大小限制
Sep 10 PHP
初识php MVC
Sep 10 PHP
Opcache导致php-fpm崩溃nginx返回502
Mar 02 PHP
PHP获取一段文本显示点阵宽度和高度的方法
Mar 12 PHP
[原创]ThinkPHP中SHOW_RUN_TIME不能正常显示运行时间的解决方法
Oct 10 PHP
Smarty环境配置与使用入门教程
May 11 PHP
浅谈PHP中静态方法和非静态方法的相互调用
Oct 04 PHP
PHP-FPM和Nginx的通信机制详解
Feb 01 PHP
浅谈php调用python文件
Mar 29 PHP
PHP命令行与定时任务
Apr 01 PHP
常用表单验证类,有了这个,一般的验证就都齐了。
Dec 06 #PHP
IIS6.0+PHP5.x+MySQL5.x+Zend3.0x+GD+phpMyAdmin2.8x通用安装实例(已经完成)
Dec 06 #PHP
PHP分页显示制作详细讲解
Dec 05 #PHP
PHP生成静态页面详解
Dec 05 #PHP
详解:――如何将图片储存在数据库里
Dec 05 #PHP
怎样才能成为PHP高手?学会“懒惰”的编程
Dec 05 #PHP
PHP开发入门教程之面向对象
Dec 05 #PHP
You might like
PHP判断搜索引擎蜘蛛并自动记忆到文件的代码
2012/02/04 PHP
给网站上的广告“加速”显示的方法
2007/04/08 Javascript
jquery中的sortable排序之后的保存状态的解决方法
2010/01/28 Javascript
jQuery()方法的第二个参数详解
2015/04/29 Javascript
微信小程序 POST请求(网络请求)详解及实例代码
2016/11/16 Javascript
JS实现一个简单的日历
2017/02/22 Javascript
Node.js搭建小程序后台服务
2018/01/03 Javascript
jQuery代码优化方法总结
2018/01/29 jQuery
关于HTML5的data-*自定义属性的总结
2018/05/05 Javascript
JS实现关键词高亮显示正则匹配
2018/06/22 Javascript
JS重学系列之聊聊new操作符
2019/03/04 Javascript
关于element-ui表单中限制输入纯数字的解决方式
2020/09/08 Javascript
python自动化测试实例解析
2014/09/28 Python
解决Django模板无法使用perms变量问题的方法
2017/09/10 Python
python中WSGI是什么,Python应用WSGI详解
2017/11/24 Python
Python实现的寻找前5个默尼森数算法示例
2018/03/25 Python
Python requests发送post请求的一些疑点
2018/05/20 Python
Pipenv一键搭建python虚拟环境的方法
2018/05/22 Python
用Python将一个列表分割成小列表的实例讲解
2018/07/02 Python
python实现图片压缩代码实例
2019/08/12 Python
Python基于进程池实现多进程过程解析
2020/04/30 Python
Python数据可视化图实现过程详解
2020/06/12 Python
Jmeter HTTPS接口测试证书导入过程图解
2020/07/22 Python
阿里巴巴国际站:Alibaba.com
2016/07/21 全球购物
行政助理岗位职责范文
2013/12/03 职场文书
千元咖啡店的创业计划书范文
2013/12/29 职场文书
应届毕业生通用的自荐书范文
2014/02/07 职场文书
银行服务感言
2014/03/01 职场文书
金融学专业大学生职业生涯规划
2014/03/07 职场文书
艺术学院毕业生自荐信
2014/07/05 职场文书
2014年党员自我评议(5篇)
2014/09/12 职场文书
争先创优演讲稿
2014/09/15 职场文书
银行领导班子四风对照检查材料
2014/09/27 职场文书
省委召开党的群众路线教育实践活动总结大会报告
2014/10/21 职场文书
医务人员岗前培训心得体会
2016/01/08 职场文书
Oracle 触发器trigger使用案例
2022/02/24 Oracle