使用PHP的PDF文件下载在服务器中被破坏

时间:2010-08-16 11:36:40

标签: php download header

我已经使用PHP标头实现了下载脚本来下载PDF文件。我的脚本工作正常,并在我的localhost中下载相应的PDF文件,但该脚本在我的服务器中不起作用。

我的代码是:

header("Content-Disposition: attachment; filename='dfile.pdf'");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: application/pdf;");

readfile("http://www.education.gov.yk.ca/pdf/pdf-test.pdf");

在我的服务器文件浏览器中显示正常并将文件下载为dfile.pdf,但如果我尝试打开下载的文件,则会收到the file has been damaged的错误消息。我的任何服务器设置导致此问题或其他?

任何解决此问题的建议都非常明显。

注意:我认为这可能是我的服务器PHP设置的问题,任何人都可以告诉我“readfile”函数是否需要在我的服务器中启用任何PHP设置?

提前致谢, 希瓦...

3 个答案:

答案 0 :(得分:3)

我认为PHP会在PDF文件正文之前输出一些警告。检查一下。

答案 1 :(得分:0)

此问题是由于我的服务器PHP设置,我的服务器中未启用“allow_url_fopen”设置。我正在使用CURL处理这种情况,无论如何,感谢您的支持。

答案 2 :(得分:0)

尝试使用此代码:

$file = 'monkey.pdf';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
相关问题