PHP下载PDF抛出错误


PHP to download PDF throws error

我有一个PHP文件来强制下载PDF文件,在一台服务器(linux)上运行良好,但当我将网站移动到另一台服务器时(Win),它开始出现以下错误:

PHP Warning:  readfile(./forms/Form.pdf)
[<a href='function.readfile'>function.readfile</a>]:
failed to open stream: No such file or directory in
D:'CustomerData'webspaces'webspace'wwwroot'Form.php
on line 4

PHP文件包含以下内容:

<?php
header('Content-disposition: attachment; filename=./forms/Form.pdf');
header('Content-type: application/pdf');
readfile('./forms/Form.pdf');
?>

谢谢!

尝试使用绝对文件位置。我认为这个错误是因为文件路径

请尝试以下代码。

$file = <absolute file path>;
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, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
print_r(headers_list());
readfile($file);
相关文章: