2017-09-11 306 views
0

我的代碼是工作的罰款。我的文件也下載但是當我打開一個文件,那麼它是不開放給了一個錯誤「錯誤 無法加載PDF文檔。」無法打開pdf文件

<?php 

    $pno = $_GET['pno']; 


    $sql = "SELECT file FROM tenders WHERE Tno = $id"; 


    $file = "data/" . $mysql_row['file ']; 

    header("Content-type:application/pdf"); 

    header("Content-Disposition:attachment;filename='downloaded.pdf'"); 

    readfile($file); 
    ?> 
+0

我會問自己,如果該文件是有效的PDF? – mplungjan

+0

@mplungjan是的。 –

+0

檢查權限 –

回答

0

我會建議總是檢查文件是否存在。如果它不readfile()最終會在您的PDF文件中出現可能導致問題的錯誤。試試看這樣:

$pno = $_GET['pno']; 
$sql = "SELECT file FROM tenders WHERE Tno = $id"; 

$file = "data/" . $mysql_row['file ']; 

if(file_exists($file)){ 
    header("Content-type:application/pdf"); 
    header("Content-Disposition:attachment;filename='downloaded.pdf'"); 
    readfile($file); 
} else { 
    echo "File does not exist!"; 
} 

另外,沒有聲明$ id變量。難道$ pno應該改爲$ id嗎?

0
$file = "path_to_file"; 
$fp = fopen($file, "r") ; 

header("Cache-Control: maxage=1"); 
header("Pragma: public"); 
header("Content-type: application/pdf"); 
header("Content-Disposition: inline; filename=".$myFileName.""); 
header("Content-Description: PHP Generated Data"); 
header("Content-Transfer-Encoding: binary"); 
header('Content-Length:' . filesize($file)); 
ob_clean(); 
flush(); 
while (!feof($fp)) { 
    $buff = fread($fp, 1024); 
    print $buff; 
} 
exit; 
+0

宕:沒有重複的接聽鍵:https://stackoverflow.com/questions/5670785/chrome-has-failed-to-load-pdf-document-error-message-on-inline-pdfs – YvesLeBorg

+0

@YvesLeBorg好的謝謝你的支持 – Aks