2015-09-07 150 views
0

我想下載使用PHP的PDF文件。我可以下載文本文件,圖像和 我PHP代碼:php pdf文件下載:無法加載PDF文檔

header("Content-Type: application/octet-stream"); 

$file = $_GET["file"] .".pdf"; 
header("Content-Disposition: attachment; filename=" . urlencode($file));  
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header("Content-Type: application/pdf"); // pdf might change to another format eg. doc,docx 
header("Content-Description: File Transfer");    
header("Content-Length: " . filesize($file)); 
flush(); // this doesn't really matter. 
$fp = fopen($file, "r"); 
while (!feof($fp)) 
{ 
echo fread($fp, 65536); 
flush(); // this is essential for large downloads 
} 
fclose($fp); 

這裏是HTML

部分:

<a href="download.php?file=abc">Download CV</a> 

在我來說,我能下載PDF文件,它也顯示內存大小,但當我打開它顯示錯誤,如:

無法加載PDF文檔

我沒有得到任何錯誤,我試圖改變內存限制和所有,但不工作。任何幫助將非常感激。

編輯:

我需要下載所有的文件格式,在我的代碼中的一些簡單的修改。 例如:

header("Content-Type: application/pdf"); // pdf might change to another format eg. doc,docx 

我並不想保存這是因爲在其他格式的我不能直接從瀏覽器保存的情況下顯示在瀏覽器的PDF文件。

當我試圖從下載打開文件夾時顯示錯誤 像:

的Adobe閱讀器無法打開的文件名abc.pdf,因爲它要麼不支持的文件類型或因爲文件已損壞

+0

如果我可能會問,爲什麼你需要這個頭信頭(「Content-Type:application/download」); – Satya

+0

[Chrome在內嵌PDF文件中有「加載PDF文檔失敗」錯誤信息](http://stackoverflow.com/questions/5670785/chrome-has-failed-to-load-pdf-document-error-message -on-inline-pdfs) – Saty

+0

@Saty不,它不能完全解決我的問題。它顯示瀏覽器上的內容,即使我可以保存,因爲它是PDF文件。但與其他格式 –

回答

0

你可以試試嗎?

更改

echo fread($fp, 65536); 

echo fread($fp, filesize($file)); 

我的猜測是PDF文件的長度爲你指定什麼不同。

+0

感謝您的答案,但它不起作用 –