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中的注释、变量、数组、常量、函数应用介绍
Nov 16 PHP
3个PHP多维数组转为一维数组的方法实例
Mar 13 PHP
ThinkPHP实现支付宝接口功能实例
Dec 02 PHP
PHP生成条形图的方法
Dec 10 PHP
PHP实现动态执行代码的方法
Mar 25 PHP
php数据访问之增删改查操作
May 09 PHP
windows7配置Nginx+php+mysql的详细教程
Sep 04 PHP
Fleaphp常见函数功能与用法示例
Nov 15 PHP
php检查函数必传参数是否存在的实例详解
Aug 28 PHP
PHPCrawl爬虫库实现抓取酷狗歌单的方法示例
Dec 21 PHP
php微信支付之公众号支付功能
May 30 PHP
PHP fclose函数用法总结
Feb 15 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生成HTML静态页面实例代码
2008/08/31 PHP
ajax php 实现写入数据库
2009/09/02 PHP
PHP中防止SQL注入实现代码
2011/02/19 PHP
微信API接口大全
2015/04/15 PHP
PHP实现分布式memcache设置web集群session同步的方法
2018/04/10 PHP
解决PHP使用CURL发送GET请求时传递参数的问题
2019/10/11 PHP
解决laravel 表单提交-POST 异常的问题
2019/10/15 PHP
js中cookie的使用详细分析
2008/05/28 Javascript
javascript new 需不需要继续使用
2009/07/02 Javascript
基于jquery的自定义鼠标提示效果 jquery.toolTip
2010/11/14 Javascript
对Jquery中的ajax再封装,简化操作示例
2014/02/12 Javascript
js和jquery中循环的退出和继续下一个循环
2014/09/03 Javascript
js单独获取一个checkbox看其是否被选中
2014/09/22 Javascript
基于jQuery实现点击弹出层实例代码
2016/01/01 Javascript
超链接怎么正确调用javascript函数
2016/05/23 Javascript
详解angular2封装material2对话框组件
2017/03/03 Javascript
Vue-CLI 3 scp2自动部署项目至服务器的方法
2020/07/24 Javascript
[01:21:36]CHAOS vs Alliacne 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
python 爬取微信文章
2016/01/30 Python
详解用python实现简单的遗传算法
2018/01/02 Python
Python实现的登录验证系统完整案例【基于搭建的MVC框架】
2019/04/12 Python
Python中使用双下划线防止类属性被覆盖问题
2019/06/27 Python
python pyenv多版本管理工具的使用
2019/12/23 Python
python实现简单俄罗斯方块
2020/03/13 Python
python shapely.geometry.polygon任意两个四边形的IOU计算实例
2020/04/12 Python
django 数据库返回queryset实现封装为字典
2020/05/19 Python
最简单的matplotlib安装教程(小白)
2020/07/28 Python
什么是servlet链?
2014/07/13 面试题
《雪儿》教学反思
2014/04/17 职场文书
给校长的建议书300字
2014/05/16 职场文书
医生爱岗敬业演讲稿
2014/08/26 职场文书
邀请函模板
2015/02/02 职场文书
2015年档案管理员工作总结
2015/05/13 职场文书
九不准学习心得体会
2016/01/23 职场文书
PHP实现考试倒计时功能代码
2021/04/16 PHP
Java并发编程之Executor接口的使用
2021/06/21 Java/Android