function downLoad(){ $path = './static/zhan/uploads/head/201708311617012941.png'; $filename = '个人文件.png'; // 文件名 //下面是输出下载; header("Cache-Control: max-age=0"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename=' . basename($filename )); // 文件名 header('Content-Type: application/octet-stream'); //设置为一个下载类型 header("Content-Transfer-Encoding: binary"); // 告诉浏览器,这是二进制文件 header('Content-Length: ' . filesize($path)); // 告诉浏览器,文件大小 @readfile($path); //输出文件; }
这段代码测试过正常的,以前 $path ='http://xxx/./static/zhan/uploads/head/201708311617012941.png'; 是写成这样的,致使下载下来的文件没法正常打开,以前觉得是文件大小缘由,最后发现我把header('Content-Length: ' . filesize($path));这句注释掉,竟然能够正常打开文件了,最后才知道filesize($path)不须要加域名。浏览器