zoukankan      html  css  js  c++  java
  • header 头各种类型文件下载

    function down_file($url,$type='application/zip'){
        header("Cache-Control: public"); 
        header("Content-Description: File Transfer"); 
        header('Content-disposition: attachment; filename='.basename($url)); //文件名   
        header("Content-Type: ".$type); //zip格式的   
        header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件    
        header('Content-Length: '. filesize($url)); //告诉浏览器,文件大小   
        @readfile($url);
    }

    个人修改为:

       //下载外部链接到客户端本地
        public function download($url)
        {$type = $this->getFileType($url);
            $size = $this -> getFileSize($url);
            header("Cache-Control: public");
            header("Content-Description: File Transfer");
            header('Content-disposition: attachment; filename='.basename($url)); //文件名
            header("Content-Type: ".$type); //zip格式的
    //        header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
            header('Content-Length: '. $size); //告诉浏览器,文件大小
            @readfile($url);
        }
    
    
       //获取外部链接文件大小
        function getFileSize($url, $user = "", $pw = "")
        {
            ob_start();
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_NOBODY, 1);
    
            if(!empty($user) && !empty($pw))
            {
                $headers = array('Authorization: Basic ' .  base64_encode("$user:$pw"));
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            }
    
            $ok = curl_exec($ch);
            curl_close($ch);
            $head = ob_get_contents();
            ob_end_clean();
    
            $regex = '/Content-Length:s([0-9].+?)s/';
            $count = preg_match($regex, $head, $matches);
    
            return $matches[1];
        }
    
    
    
        //获取外部链接文件类型
        function getFileType($url)
        {
            $file_type=explode('.',$url);
    
            $file_type=$file_type[count($file_type)-1];
    
            return $file_type;
        }
  • 相关阅读:
    js判断是否是ie浏览器且给出ie版本
    自定义的好看的单选复选框功能
    Gaze Estimation学习笔记(2)-It's Written All Over Your Face Full-Face Appearance-Based Gaze Estimation
    Gaze Estimation学习笔记(1)-Appearance-Based Gaze Estimation in the Wild
    软件工程个人总结博客
    软件工程结对编程博客
    软件工程第一次阅读作业
    软工第0次个人作业
    OO第四次博客作业
    OO第三次博客作业
  • 原文地址:https://www.cnblogs.com/laijinquan/p/13385659.html
Copyright © 2011-2022 走看看