刚刚学习PHP,做一个强制下载文件时,测试了一下PDF强制下载,居然提示502...
这个是代码,反复确认是没问题的...
//让浏览器强制下载文件
function download($filepath){
if ((isset($filepath))&&(file_exists($filepath))){
header("Content-length: ".filesize($filepath));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . substr($filepath,strrpos($filepath, '/')+1, strlen($filepath)) . '"');
readfile("$filepath");
} else {
echo "Looks like file does not exist!";
}
}
最后发现问题是PhpStorm 2018.1.6 版本内置服务器有问题,直接运行测试一直报这个502错...
我更改成Apache服务器再测试就没问题了。
下面地址是我参考更改Apache服务器的地址: