2013-12-23 64 views
0

以下是代碼:爲什麼我無法下載已經存在於服務器上的pdf文件?

define(ADMIN_ROOT,'/var/www/abc/pqr/web/control/'); 
$filename = $test_data['test_name']; 
$flname = ADMIN_ROOT.'modules/tests/test_pdfs/'.$filename; 
header("Content-Type: application/pdf"); 
header("Cache-Control: no-cache"); 
header("Accept-Ranges: none");  
header("Content-Disposition: attachment; filename=\"" .$flname. ".pdf\""); 

PDF文件已經存在在那邊目錄

/var/www/abc/pqr/web/control/modules/tests/test_pdfs/ 

而且它擁有所有權限assigned.But當我嘗試下載文件時,它的下載一些具有相同名稱的不同文件,但該文件不是可以打開的格式。總之,所需的文件沒有被下載。任何人都可以幫助我糾正我的問題嗎?

回答

0

請試試這個

<?php 
$filename = $test_data['test_name']; 
$file = dirname(__FILE__) . "/modules/tests/test_pdfs/" . $filename; 

//$file = "/var/www/htdocs/audio/" . $filename; 


       if (file_exists($file)) { 
         header ("Content-type: octet/stream"); 
         header ("Content-disposition: attachment; filename=" . str_replace(" ", "_", basename($file)) . ";"); 
         header ("Content-Length: " . filesize($file)); 
         readfile($file); 
         die(); 
       } 
       else { 

        //ERROR MESSAGE HERE.......... 
       } 
?> 

請改變這一行

header("Content-Type: application/pdf"); 

header ("Content-type: octet/stream"); 
0
You can also use the following code for download any type of file extensions: 
<?php 
$filename = $test_data['test_name']; 
$contenttype = "application/force-download"; 
header("Content-Type: " . $contenttype); 
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";"); 
readfile(ADMIN_ROOT.'modules/tests/test_pdfs/'.$filename); 
exit(); 
?> 
相關問題