2016-07-25 70 views
0

我正在使用以下php代碼來下載pdf文件。我可以下載PDF文件但無法打開它。我做錯了什麼?誰能幫我?

的download.php無法打開使用php下載的PDF文件

<?php 
$file = 'Rev.pdf'; 
$filepath = "doc/" . $file; 
header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream; charset=utf-8'); 
header('Content-Disposition: attachment; filename='.basename($filepath)); 
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($filepath)); 
ob_clean(); 
flush(); 
readfile($filepath); 
exit; 
?> 

更新時間:
當我嘗試下載瀏覽器上後,打開它,我得到以下錯誤。 enter image description here

UPDATE2:
以下是我的HTML

<a href="download.php">download</a> 

UPDATE3:解決
首先我想說抱歉讓你所有的煩惱。我已經解決了這個問題。其實我的PDF文件本身已損壞。所以我創建了新的pdf文件,並且運行良好。再次感謝大家聽到我的問題並提出建議。

+0

當你打開它時會發生什麼? –

+0

我已更新我的問題添加錯誤消息。我可以從瀏覽器或下載的文件夾中打開它。 – NajLinus

+0

保存文件,然後使用文本編輯器在本地打開它,檢查php錯誤的頂部和底部 – 2016-07-25 01:29:01

回答

1

而不是

header('Content-Type: application/octet-stream; charset=utf-8'); 

試試這個

header('Content-Type: application/pdf'); 

而且閱讀本:

+0

你好Michael_B,我在文件名上使用日文字符時遇到了問題。我的文件名是英文和日文字符的混合。例如$ file =「form申し込み用(data-form)_DOP.pdf」。當我將文件名更改爲英文時,一切正常。但是,當我混合日本和英文字符同樣的錯誤發生,我已經提到上面。我認爲問題發生是因爲我的PDF文件已損壞。但這似乎並不是真正的問題。我已經重新創建了PDF文件。你能給我任何建議嗎? – NajLinus

+1

http://stackoverflow.com/q/666987/3597276 –

+1

http://stackoverflow.com/q/10006642/3597276 –