2012-07-31 71 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