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
最后
以上就是着急未来最近收集整理的关于PHP头文件下载文件的小坑的全部内容,更多相关PHP头文件下载文件内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复