文件下载类大全

<?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;
            }
        }

    }
}

?>

文件下载类大全

时间: 2024-10-14 19:44:26

文件下载类大全的相关文章

File 文件操作类 大全

File  文件操作类  大全 许多人都会对文件操作感到很难  我也是  但是一个好的项目中必定会涉及到文件操作的 文件的复制 粘贴  等等等 公司大佬写了 一个文件操作的工具类 感觉还是棒棒的啦   代码如下 : 1 /** 2 * Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. 3 */ 4 package

php 支持断点续传的文件下载类

php 支持断点续传的文件下载类 分类: php class2013-06-30 17:27 17748人阅读 评论(6) 收藏 举报 php断点续传下载http测试 php 支持断点续传,主要依靠HTTP协议中 header HTTP_RANGE实现. HTTP断点续传原理Http头 Range.Content-Range()HTTP头中一般断点下载时才用到Range和Content-Range实体头,Range用户请求头中,指定第一个字节的位置和最后一个字节的位置,如(Range:200-3

FileDown(文件下载类)

using System;using System.IO;using System.Threading;using System.Web; /// <summary>/// 文件下载类/// </summary>public class FileDown{    public FileDown()    { } /// <summary>    /// 参数为虚拟路径    /// </summary>    public static string Fil

最完整PHP常用工具类大全

<?php /** * 助手类 * @author www.shouce.ren */ class Helper { /** * 判断当前服务器系统 * @return string */ public static function getOS(){ if(PATH_SEPARATOR == ':'){ return 'Linux'; }else{ return 'Windows'; } } /** * 当前微妙数 * @return number */ public static funct

C# 文件操作类大全

1.创建文件夹 //using    System.IO;   Directory.CreateDirectory(%%1); 2.创建文件 //using   System.IO;   File.Create(%%1); 3.删除文件夹//using System.IO;Directory.Delete(%%1); 4.删除文件  //using   System.IO;  File.Delete(%%1); 5.删除一个目录下所有的文件夹 1 //using 2 System.IO; 3 f

Android 工具类大全java文件

package com.clw.cosmax.utils; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncoding

css伪类大全用法详解

CSS伪类及伪元素选择器,如超链接的a:link.a:visited.a:hover等等, 一.超链接 a:link{ color:red;} a:visited{ color:blue;} a:hover{ color:black;} a:active{ color:#6600CC;} 注意:设置的顺序不能变,遵循爱恨(love/hate)原则. ——此原则自己在网上看别人说的,便于记忆就写在这了 二.更多伪类 :root   ------------------------------ 文档

C# 各种帮助类大全

前言 此篇专门记录一些常见DB帮助类及其他帮助类,以便使用时不用重复造轮子. DBHelper帮助类 ①首当其冲的就是Sql Server帮助类,创建名为DbHelperSQL 的类 ,全部代码如下: /// <summary> /// 数据访问抽象基础类 /// Copyright (C) Maticsoft /// </summary> public abstract class DbHelperSQL { //数据库连接字符串(web.config来配置),多数据库可使用Db

php实现的支持断点续传的文件下载类

通常来说,php支持断点续传,主要依靠HTTP协议中 header HTTP_RANGE实现. HTTP断点续传原理: Http头 Range.Content-Range()HTTP头中一般断点下载时才用到Range和Content-Range实体头,Range用户请求头中,指定第一个字节的位置和最后一个字节的位置,如(Range:200-300)Content-Range用于响应头 请求下载整个文件: GET /test.rar HTTP/1.1 Connection: close Host: