2014-09-26 68 views
0
$file = "http://www.abax.se/content/download/137/862/file/example.pdf"; 
header('Content-Description: File Transfer'); 
header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
flush(); 
readfile($file); 

這是我正在運行的代碼,我出來的想法,任何解決方案?PHP下載給我空PDF

+1

刪除'平齊()'? – Daan 2014-09-26 13:30:39

+0

同樣的東西,空的下載pdf ... – josaric 2014-09-26 13:38:01

+0

這段代碼看起來很奇怪。爲什麼要下載文件才能上傳?你爲什麼不直接發送「301永久移動」響應,將用戶直接發送到文件?該文件是否與此代碼位於同一臺服務器上?如果是這樣,那麼你不應該通過HTTP;您應該直接從服務器讀取並輸出文件。 – 2014-09-26 13:38:08

回答

0

刪除header('Content-Length: ' . filesize($file));

那是什麼打破你的輸出。

filesize()需要一個本地文件,而不是一個url。

工作代碼:

<?php 
$file = "http://www.abax.se/content/download/137/862/file/example.pdf"; 
header('Content-Description: File Transfer'); 
header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment; filename='.basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate'); 
header('Pragma: public'); 
flush(); 
readfile($file); 
?> 
+0

「無法加載PDF文檔」 – josaric 2014-09-26 13:39:14

+0

它雖然在這裏工作.. – 2014-09-26 13:40:25

+0

添加了工作代碼的答案。這在php5.4上工作正常。 – 2014-09-26 13:45:46