本文介绍了PDF下载功能不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码下载PDF ....

I am using the following code to download a PDF....

   header('Content-Type: application/pdf');
   header('Content-Disposition: attachment; filename="NewName.pdf"');
   readfile($root_path.'/full-tile-book.pdf');

这是一个13 MB的文件,只下载130k,当然不能打开。如何调试问题在这里?我的路径是正确的。

It's a 13 MB file and only downloads like 130k and of course can't be opened. How do I debug what the issue is here? My path to the pdf is correct.

推荐答案

也许你遇到了内存/文件大小错误?我以前在使用readfile转储大文件时遇到了问题。除了设置Content-Length标题之外,我建议您使用,因为它没有将文件读入缓冲区,它只是将其转储。

Perhaps you are running into a memory/filesize error? I've had problems in the past dumping large files with readfile. In addition to setting the Content-Length header, I recommend using fpassthru(), as it does not read the file into a buffer, it just dumps it.

set_time_limit(0); // disable timeout
$file = $root_path.'/full-tile-book.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="NewName.pdf"');
header('Content-Length: ' . filesize($file));
session_write_close(); // remove this line if sessions are not active
$f = fopen($file, 'rb');
fpassthru($f);
fclose($f);
exit;

编辑:如果您正在使用任何sessions_code,在开始之前结束会话是个好主意文件转储过程。我已经更新了我的例子来反映这一点。

If you are using any sessions_code, it is a good idea to end the session before starting the file dump process. I have updated my example to reflect this.

这篇关于PDF下载功能不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:33