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;
        }
  • 相关阅读:
    spring中@Lookup的作用
    spring中的观察者模式
    spring事务源码解析
    spring中@Configuration注解的作用
    HTML5和CSS3
    TCP/IP协议
    Android项目的settings.gradle和build.gradle
    AndroidManifest.xml 最全详解
    Android实现网络监听
    Android数据存取
  • 原文地址:https://www.cnblogs.com/laijinquan/p/13385659.html
Copyright © 2011-2022 走看看