温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

一个PHP文件下载类

发布时间:2020-07-12 01:37:02 来源:网络 阅读:465 作者:baoying1989920 栏目:web开发
class downLoad{
    var $file_name;
    var $file_dir;
    var $buffer_size = 1024;
    var $err = "";
    public static $MIME_type = array(
                                "pdf"  =>"application/pdf",
                                "exe"  =>"application/octet-stream",
                                "zip"  =>"application/zip",
                                "doc"  =>"application/msword",
                                "xls"  =>"application/vnd.ms-excel",
                                "ppt"  =>"application/vnd.ms-powerpoint",
                                "gif"  =>"p_w_picpath/gif",
                                "png"  =>"p_w_picpath/png",
                                "jpeg" =>"jpg",
                                "mp3"  =>"audio/mpeg",
                                "wav"  =>"audio/x-wav",
                                "mpeg" =>"mpg",
                                "mpe"  =>"video/mpeg",
                                "mov"  =>"video/quicktime",
                                "avi"  =>"video/x-msvideo",             
                            );
    public function __construct($file_dir="",$file_name=""){
        $this->file_dir  = $file_dir;
        $this->file_name = $file_name;
        $this->path = $file_dir."/".$file_name;
        $this->suffix = pathinfo($file_name,PATHINFO_EXTENSION);
    }
      
    public function down(){
        if(!file_exists($this->path)){
            $this->err = "该文件被移除了";
            return false;
        }
        $content_type = $this->getMIME($this->suffix);
        $file_size = filesize($this->path);
          
        header("Content-type: ".$content_type);
        header('Content-Disposition: p_w_upload; filename="'.$this->file_name.'"');
          
        @header("Cache-control: public");
        @header("Pragma: public");
        header("Content-Length: ".$file_size);
        ob_end_clean();
        //readfile($this->path); 一次性读出来
        $fp= fopen($this->path,"r");
        $buffer_size = $this->buffer_size;
        $cur_pos = 0; //记录读了多少了
          
        while(!feof($fp) && $file_size>$buffer_size+$cur_pos){
            $buffer = fread($fp,$buffer_size); //每次读1024字节
            echo $buffer;
            $cur_pos += $buffer_size;
        }
        //把剩下的读出来 因为文件的带下很有很能不是1024 的整数倍
        $buffer = fread($fp,$file_size-$cur_pos);
        echo $buffer;
        fclose($fp);
        return true;       
    }
      
    public function getMIME($key=""){
        if($key == "" || !isset(self::$MIME_type[$key])){
            return "application/octet-stream";
        }
        return self::$MIME_type[$key];
    }
}
//  $x = new downLoad($file_dir,$file_name);  $file_dir路径 比如 all     $file_name文件名 比如 a.exe  合起来就是全部的路径了all/a.exe
//  $x->down();


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI