文件下载类大全

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/* $download = new download('php,exe,html',false);

$this->load->library('Download');
if(!$this->download->->downloadfile($filename))
{
    echo $this->download->geterrormsg();
}
*/    

class Download
{
  var $debug=true;
  var $errormsg='';
  var $Filter=array();
  var $filename='';
  var $_realname = '';
  var $mineType='text/plain';
  var $filetype ='';
  var $xlq_filetype=array();
  protected $CI = NULL;
  
 
  function __construct($fileFilter='',$isdebug=false)
  {
          $this->CI = &get_instance();
        $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,$_realname)
  {
    $this->filename = mb_convert_encoding(trim($filename),"Big5");
    $this->_realname = empty($_realname) ? $this->filename : $_realname;
  }
  
  function downloadfile($filename,$_realname='',$schdule=false)
  {
    
    $this->setfilename($filename,$_realname);
    if($this->filecheck($schdule))
    {
        //重新設置文件名稱 edited by zcs 
        $fn = !empty($_realname) ? $this->trim_special_str($_realname) : array_pop( explode( '/', strtr( $this->filename, '\\', '/' ) ) );
        
        if(!empty($_realname) && strtolower(array_pop(explode('.',$fn))) !== $this->filetype){
            $fn .= '.'.$this->filetype;
        }        
        $this->filename = str_replace(' ','%20', $this->filename);
        
        header( "Content-type: text/plain; charset=utf-8");
        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 );
        if(!$schdule){
            header( "Content-Length: " . get_file_size( $this->filename ) );
        }
        if(preg_match("/MSIE/i", $_SERVER["HTTP_USER_AGENT"])){
            header( 'Content-Disposition: attachment; filename="'.urlencode($fn).'"' );//解决IE中文乱码
        }else{
            header( 'Content-Disposition: attachment; filename="'.$fn.'"' );//解决IE中文乱码
        }    
        header( 'Content-Transfer-Encoding: binary' );
        readfile( $this->filename );
        return true;
    }
    else
    {
        
        return false;
    }
  }
  //add by zcs
  function trim_special_str($_str)
  {
     return str_replace(array('/','?','*',':','"','<','>','\\'),array('','','','','','','',''),$_str);
   }
   

   
  function geterrormsg()
  {
    return $this->errormsg;
  }
  
  function filecheck($schdule)
  {
    $filename = $this->filename;
    $lang_test = $this->CI->lang->language;
    header( "Content-type: text/html; charset=utf-8");
        if(es_file_exists($filename))
        {
           $this->filetype=strtolower(array_pop(explode('.',$filename)));
           if(in_array($this->filetype,$this->Filter))
           {
             $this->errormsg.=$filename.$lang_test['file_forbbing_down'];
                 if($this->debug) exit($filename.$lang_test['file_forbbing_down']) ;
                 return false;
           }else
           {
               if(!$schdule){
                    $this->file_mime_type($filename);
               }else{
                     $this->mineType = 'application/msword'; 
                 }
                 if(empty($this->mineType))
                 {
                    $this->mineType = isset($this->xlq_filetype[$this->filetype]) ? $this->xlq_filetype[$this->filetype] : 'application/octet-stream';
                 }
                 if(!empty($this->mineType))
                   return true;
                 else
                 {
                    $this->errormsg.= $filename.$lang_test['get_file_type_error'];
                        if($this->debug) exit($lang_test['get_file_type_error']);
                        return false;
                 }
           } 
        }else
        {
          $filename = iconv("Big5","UTF-8",trim($filename));//防止乱码
          $this->errormsg.=$filename.$lang_test['file_not_found'];
          if($this->debug) exit($filename.$lang_test['file_not_found']) ;
          return false;
        }
  }
  
  function setfiletype()
  {
    $this->xlq_filetype['chm']='application/octet-stream';
    $this->xlq_filetype['ppt']='application/vnd.ms-powerpoint';
    $this->xlq_filetype['odp']='application/vnd.ms-powerpoint';
    $this->xlq_filetype['xls']='application/vnd.ms-excel';
    $this->xlq_filetype['doc']='application/msword';
    $this->xlq_filetype['pptx']='application/vnd.ms-powerpoint';
    $this->xlq_filetype['pps']='application/vnd.ms-powerpoint';
    $this->xlq_filetype['xlsx']='application/vnd.ms-excel';
    $this->xlq_filetype['docx']='application/msword';
    $this->xlq_filetype['odt']='application/msword';
    $this->xlq_filetype['ods']='application/vnd.ms-excel';
    $this->xlq_filetype['ws']='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";
  }
  
  
  function file_mime_type($file)
    {
        $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
        if (function_exists('finfo_file'))
        {
            $finfo = finfo_open(FILEINFO_MIME);
            if (is_resource($finfo)){
                $mime = @finfo_file($finfo, $file);
                finfo_close($finfo);
                if (is_string($mime) && preg_match($regexp, $mime, $matches)){
                    $this->mineType = $matches[1];
                    return;
                }
            }
        }

        if (DIRECTORY_SEPARATOR !== '\\'){
            $cmd = 'file --brief --mime ' . escapeshellarg($file) . ' 2>&1';

            if (function_exists('exec')){
                $mime = @exec($cmd, $mime, $return_status);
                if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches)){
                    $this->mineType = $matches[1];
                    return;
                }
            }

            if ( (bool) @ini_get('safe_mode') === FALSE && function_exists('shell_exec')){
                $mime = @shell_exec($cmd);
                if(strlen($mime) > 0){
                    $mime = explode("\n", trim($mime));
                    if (preg_match($regexp, $mime[(count($mime) - 1)], $matches)){
                        $this->mineType = $matches[1];
                        return;
                    }
                }
            }

            if (function_exists('popen')){
                $proc = @popen($cmd, 'r');
                if (is_resource($proc)){
                    $mime = @fread($proc, 512);
                    @pclose($proc);
                    if ($mime !== FALSE){
                        $mime = explode("\n", trim($mime));
                        if (preg_match($regexp, $mime[(count($mime) - 1)], $matches)){
                            $this->mineType = $matches[1];
                            return;
                        }
                    }
                }
            }
        }

        // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
        if (function_exists('mime_content_type')){
            $this->mineType = @mime_content_type($file);
            if (strlen($this->mineType) > 0){
                return;
            }
        }

    }
}


?>

 

转载于:https://www.cnblogs.com/timily/p/3899416.html

网友netstarry写的一个处理字符编码的,很好的解决了php中字符转换的问题 前一阵子见到了qiushuiwuhen君的关于gbk,unicode,big5的转换的文章 但是多少有一些不太大的问题 于是我设计了一个负责字符转换的,修正了其中的一些不足,增加了部分功能,以后我会不断扩充该,来支持更多的字符集 增加了如下几点: unicode->gbk 符号部分的转换 欧元符(?的识别 big5,Unicode,GBK之间的相互转换,前提是只转换共同的字符集部分, 使用说明: 暂时程序支持以下字符编码方式: GBK,BIG5,UTF-16BE(Unicode big-endian字节顺序),UTF-16LE(Unicode little-endian字节顺序),UTF-8 默认输入编码方式为GBK,默认输出编码方式为UTF-16BE; 该别提供两个函数用来修改输入和输出编码方式: 修改输入编码方式 boolean SetGetEncoding(string $GetEncoding) 修改输出编码方式 boolean SetToEncoding(string $ToEncoding) 函数参数只能使用上述5种编码方式,区分大小写,如GBK不能写成gbk 如果设置成功,返回true,如果使用了错误的编码名,返回false,并现实错误信息 函数 string EncodeString(string $String) 负责进行字符编码转换,返回转换后的字符串 使用前,请将var $FilePath=\"\"变量该为该程序文件的绝对路径,否则将会找不到数据文件 例子: 将gbk编码的字符串转化为UTF-8编码: $s=\"GBK编码\"; $CharEncoding=new Encoding(); $CharEncoding->SetGetEncoding(\"GBK\")||die(\"编码名错误\"); $CharEncoding->SetToEncoding(\"UTF-8\")||die(\"编码名错误\"); echo $CharEncoding->EncodeString($s); 在浏览器中使用UTF-8编码察看,将会看到正确的字符.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值