2016-12-28 132 views
0

現在我正在開發codeigniter網站。 我有一個問題。 爲了讀取&打開PDF到網頁瀏覽器。我有PHP網址問題閱讀pdf

header("Content-type:application/pdf");   
header('Content-Type: application/vnd.ms-excel'); //mime type 
header('Cache-Control: max-age=0'); //no cache   
header("Content-Disposition:attachment;filename=\"".$file."\""); 
readfile($filePath); 

上面的代碼$ file是文件名,$ filePath是pdf文件路徑。 當它在本地服務器上運行時,$ filePath的值爲「http://localhost/.pdf」,它運行良好。 但在託管服務器上運行時,此值爲「http:// .com/***。pdf」 並且不運行。 我們無法打開pdf格式的錯誤。 文件內容不包括在內。 我知道這是造成網址問題的原因。 但我沒有問題!

+1

您介意爲什麼要定義2個Content-Type頭? – josephting

+0

是否在實時服務器上獲得'$ file'和'$ filePath'值? –

+0

還請確保您沒有任何文件權限問題以防萬一。 –

回答

0

Content-Disposition應該是inline如果你想顯示在瀏覽器中,而不是作爲一個下載文件,但我想這並不重要,如果你能得到它的工作。

如果您在PHP中的allow_url_fopen配置設置爲關閉,您將無法從PHP腳本中讀取URL文件。

無論如何,你的代碼應該看起來像這樣。

<?php 

$file = "lesson2.pdf"; 
$filePath = "http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf"; 

header("Content-Type: application/pdf"); 
header("Cache-Control: max-age=0"); 
header("Content-Disposition: inline; filename=\"" . $file . "\""); 
readfile($filePath); 
+0

結果也是錯誤的。「localhost」url運行良好,但是「http://www.example.com/test.pdf」則發生錯誤。 –

+0

@BennySudibyo檢查你的PHP配置'allow_url_fopen'。 – josephting