• 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;
        }
  • 相关阅读:
    android开发(1) 登录界面的布局演示
    android开发(5) 动态生成控件
    android开发(6) 初遇handler
    android开发(2) 九宫格的实现
    android开发(8) 选项卡的切换
    android开发(9) 渐变动画演示(Tween Animation)
    android开发(8) 使用ViewFlipper来用手势切换画面
    css textarea固定大小不显示滚动条
    css zindex
    js ie input onchange事件兼容BUG
  • 原文地址:https://www.cnblogs.com/laijinquan/p/13385659.html
Copyright © 2020-2023  润新知