2012-07-31 57 views
0

我有一个PHP文件强制下载PDF文件,在一台服务器(Linux)上它工作正常,但当我将该网站移动到另一台服务器(Win)时,它开始出现此错误:PHP下载PDF抛出错误

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'); 
?> 

谢谢!

+0

maybe'filename = ../forms/Form.pdf'? – k102 2012-07-31 09:21:15

+0

它可能只是反斜杠vs(向前)斜线? – Nanne 2012-07-31 09:21:25

+0

也许该文件不存在于froms路径中,或者您的web服务器标识在路径上没有权限?您是否尝试过使用表单的绝对路径?错误是否持续? – Hinek 2012-07-31 09:22:23

回答

0

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

请尝试下面的代码。

$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); 
+0

设置“Content-length”标头时要小心 - 在与HTTP压缩结合使用时,过去遇到过问题。 – Matthew 2012-07-31 09:59:46

+0

不,还是不行,但是谢谢。 – acidking 2012-07-31 10:03:10