2016-12-05 16 views
0

我已經將文件上傳到我的webroot文件夾以外的文件夾,並使用一些php代碼在通過數據庫驗證後檢索文件。我的代碼似乎工作,即檢索文件,我唯一的問題是,上傳的pdf包含空格的文件名和下面的代碼似乎只檢索空間前的第一個單詞。檢索根外的文件,避免切斷包含空格的文件名

示例文件名是 我到London.pdf 野生動物攝影部分次數1.pdf

除了通過數據庫和文件phuycical去而代以每個文件包含下劃線代替空間,有沒有一種方法,我可以改變下面的代碼,所以它檢索整個文件名?

我想我可以用%20代替文件名,如果它檢測到一個空間,但不起作用。

/* Done some processing above to retrieve the $filename for the pdf */ 

$file = 'h:'.$filename; 


header('Content-Description: File Transfer'); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename=' . basename($file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 
header('Content-Length: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file); 
exit; 

回答

1

上傳的PDF文件包含文件名用空格和下面的代碼似乎在空間之前只檢索的第一個字。

報價名稱:

header('Content-Disposition: attachment; filename="' . basename($file) . '"'); 

或更清晰的形式:

header(sprintf('Content-Disposition: attachment; filename="%s"', basename($file))); 
相關問題