2013-03-08 70 views
0
<?php 

$file = $_GET['name']; 

$path = './curr/'.$file.'.pdf'; // the file made available for download via this PHP file 
$mm_type="application/pdf"; // modify accordingly to the file type of $path, but in most cases no need to do so 

header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Type: " . $mm_type); 
header("Content-Length: " .(string)(filesize($path))); 
header('Content-Disposition: attachment; filename="'.basename($path).'"'); 
header("Content-Transfer-Encoding: binary\n"); 

readfile($path); // outputs the content of the file 

?> 

這是file.php中的一段代碼。我指的是使用文件:當只需要pdf時,包含.php文件的PHP文件下載

<a href="file.php?name=First File">File 1</a> 

這樣做的目的是,在鏈接的點擊,./curr/First File.pdf應該下載。我確實得到了一個下載,但在檢查時,它是嵌入在文件中的pdf的網頁。任何人都可以協助

+0

上面的代碼包含'file.php'文件中的所有內容? – Sirko 2013-03-08 14:00:29

+0

否。圍繞下載有一個菜單系統等。以及登錄驗證。我應該失去所有,但PHP代碼? – Glenn 2013-03-08 14:03:40

+1

@Splatter如果你在頁面上有其他的HTML,那將會在downlaod中得到輸出。你的'file.php'根本不應該有_ANY_標記。 – 2013-03-08 14:04:08

回答

1

如果你想剛纔的PDF加載,上面的代碼將被執行的所有代碼。

刪除所有周圍的菜單,頁眉或頁腳。確保在調用此鏈接時,除了來自readfile()的PDF之外,沒有HTML或任何其他輸出。

+0

謝謝你的幫助。 – Glenn 2013-03-08 14:16:20

0

嘗試改變內容類型:

header("Content-Type: application/force-download"); 
+0

我也一樣。 PHP的.php文件沒有了。其中這個PHP是是%PDF-1.6% âãÏÓ 557 0 OBJ << /線性化1/L 1413408/O 559/E 279613/N 9/T 1412909/H [483 382] >> endobj 567 0 obj等等 – Glenn 2013-03-08 14:02:44

+2

我討厭這麼多。作爲用戶,當我點擊PDF鏈接時,我希望它在我的瀏覽器中打開。當網站迫使我下載可以在瀏覽器中查看的東西時,我絕對鄙視。我可以下載它,如果我想 - 完全脫離主題,我知道。 – 2013-03-08 14:03:31

+0

我很欣賞這一點。但該網頁也有一個部分可以在網站上查看PDF(嵌入爲對象,我承認)。這是特別的下載部分。 – Glenn 2013-03-08 14:07:32