下载PDF - 失败:网络错误,PHP-FPM和Nginx

时间:2016-12-23 12:06:55

标签: php pdf nginx download

我有一个非常简单的下载pdf的脚本:

$path = __DIR__.'/sample_file.pdf';
$pathinfo = pathinfo($path);
$fsize = filesize($path);
$filename = $pathinfo['basename'];

header('Content-Type: application/pdf');
header("Pragma:public");
header("Content-Disposition:attachment;filename=" . $filename);
header('Content-Length: ' . $fsize);
header("Pragma: no-cache");
@readfile($path);
exit;

这适用于小于10mb的文件。但是任何事情都会导致错误并且下载失败。已在多个浏览器中尝试并获得类似的结果。在所有情况下,下载都会失败。

Chrome:Failed: Network error

Firefox:[filename].pdf.part could not be saved, because the source file could not be read.

Opera:Interrupted: Network error

IE(10):[filename].pdf couldn't be downloaded.

我知道文件路径是正确的,否则它不适用于小于5mb的文件。

readfile上阅读php文档(http://php.net/manual/en/function.readfile.php),有人建议在调用output_buffering之前禁用4096(目前在php.ini中设置为readfile) 。我还没试过,但我不相信这是解决方案。

zlib.output_compression已停用。我的日志中没有看到任何错误。我也没有在Chrome检查器的网络窗格中看到任何错误。

我尝试使用chunk下载但得到相同的结果。我研究了类似的答案,但似乎都是浏览器特定的,即在某些浏览器中工作但不在其他浏览器中工作。

1 个答案:

答案 0 :(得分:0)

也许你使用暂停或类似的东西,你的目标文件是锁定。
您的安全软件可能正在扫描并锁定文件,暂停下载后该文件可能仍在系统临时文件夹中。