PHP头文件下载文件的小坑

来源:互联网 发布:网络上找不到打印机 编辑:程序博客网 时间:2024/05/22 06:37
    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)不需要加域名

其他header函数之设置content-type

原创粉丝点击