2012-12-12 158 views
2
$path = BASE_URL."/pdf/"; 
$filename= $path.basename($_GET['download_file']); 

header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header('Content-disposition: attachment; 
filename='.basename($filename)); 
header("Content-Type: application/pdf"); 
header("Content-Transfer-Encoding: binary"); 
header('Content-Length: '. filesize($filename)); 
readfile($filename); 
exit; 

這段代碼有效,但是我在打開下載的pdf時收到了Error in reading pdf file。在上面的代碼中,我得到的位置http://localhost//eec//pdf/CV_Prabin Mishra.pdf在php下載pdf的文件代碼

回答

1

BASE_URL有可能在最後一個斜槓的文件,這樣你就不會需要額外的一個:

$path = BASE_URL."pdf/"; 
1

我看不出你曾經設置$ filename的值,你在$ fullPath中設置PDF的位置,然後用$ filename讀出它。我認爲代碼應該是

$path = BASE_URL."/pdf/"; 
$fullPath = $path.basename($_GET['download_file']); 

header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header('Content-disposition: attachment; 
filename='.basename($fullPath)); 
header("Content-Type: application/pdf"); 
header("Content-Transfer-Encoding: binary"); 
header('Content-Length: '. filesize($fullPath)); 
readfile($fullPath); 
exit;