2014-04-02 46 views
0

我在存儲服務器上有一些pdf文件,我想向用戶展示它們而不顯示它們的真實文件路徑,我想用PHP來做到這一點。用PHP顯示PDF不能正確顯示

我tryed這一點:

$file = 'https://storage.server_test.com/agc/catalogs/catalog1.pdf'; 
$filename = 'catalog.pdf'; 

header('Content-type: application/pdf'); 
header('Content-Disposition: inline; filename="' . $filename . '"'); 
header('Content-Transfer-Encoding: binary'); 
header('Content-Length: ' . filesize($file)); 
header('Accept-Ranges: bytes'); 

@readfile($file); 

但都沒有顯示PDF和我得到這個錯誤:This PDF document might not be displayed correctly.

我到底做錯了什麼?

+0

您是否嘗試過使用本地文件? – Maerlyn

+0

不,因爲這些文件沒有存儲在本地。它們存儲在存儲服務器上,所以我只有它們的絕對路徑。 –

+0

你是否檢查過'filesize'和'readfile'與預期的非本地文件一起工作? – Maerlyn

回答

0

您是否試過使用html object標記嵌入它?

<object data="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf"> 
    <embed src="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf" /> 
</object> 

或者在PHP中,試試這個標題:

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

最後,如果@readfile($file)不能正常工作,請嘗試:

echo @file_get_contents($file); 

本地嘗試此我自己,我我認爲我設法讓它工作。試試這個:

<?php 

$remote_pdf = 'http://www.saronicferries.gr/content/pdfFiles/sample.pdf'; 
$remote_file_name = end(explode('/', $remote_pdf)); 
header('Content-type: application/pdf'); 
header('Content-Disposition: inline; filename="'. $remote_file_name .'"'); 
header('Content-Transfer-Encoding: binary'); 
readfile($remote_pdf); 

?> 

輸出:

enter image description here

我覺得是什麼問題是,filesize($file)是失敗的遠程文件。刪除後和header('Accept-Ranges: bytes');,它的作品。

+0

正如我所說,我想從PHP做到這一點,因爲我不希望用戶看到文件的真實路徑。 –

+0

答覆已更新。嘗試閱讀遠程內容的標題或方式。 – Latheesan

+0

我已經嘗試了最後一行,並得到如下所示的內容:%PDF-1.6%Ó39 3948 0 obj <> streamhÞTʱÃ_q_