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