当前位置:首页 > 软件开发学习 > PHP学习 > 正文内容

php下载文件类

admin6年前 (2014-12-24)PHP学习912
  <?
  //====================================================
  // 使用范例:
  // $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";
  }
  }
  ?>


扫描二维码推送至手机访问。

版权声明:本文由学无止境-开拓创新-ipvb学习网发布,如需转载请注明出处。

分享给朋友:

相关文章

php过滤危险html代码

用PHP过滤html里可能被利用来引入外部危险内容的代码。有些时候,需要让用户提交html内容,以便丰富用户发布的信息,当然,有些可能造成显示页面布局混乱的代码也在过滤范围内。以下是引用片段:#用户发...

PHP header() 函数

  PHP header() 函数  PHP HTTP 函数  定义和用法  header() 函数向客户端发送原始的 HTTP 报头。  认识到一点很重要,即必须在任何实际的输出被发送之前调用 he...

php的header来定义一个php页面为utf编码或GBK编码

  php的header来定义一个php页面为utf编码或GBK编码  php页面为utf编码  header("Content-type: text/html; charset=utf-8...

php的session优化

php的session优化

这个问题很多做php开发朋友应该都有遇到过,一个启用了session_start 页面,由于执行时间过长。导致通一个用户访问,另外一个很简单的启用session_start页面一直阻塞着。 直到第一个...

计算并输出1000000次字符串比较和数字比较的耗时

//9.代码:计算并输出1000000次字符串比较和数字比较的耗时。 $str1="123"; $str2="145"; $a=microtime(tru...

PHP语言的优势?

 随着移动互联网的兴起,互联网行业正向更加智能化的Web3.0时代迈进,中国互联网 行业进入了高速发展的势态,PHP语言已经为大部分企业广泛应用和重视(如:新浪、百度、腾讯、TOM、淘宝、搜...