2013-07-26 31 views
1

下載其中文件名有&的文件問題。PHP下載逃跑&在文件名中下載

例如,一個名爲bla的文件將會正常下載,但bla & Blaaaaa找不到該文件。

有沒有辦法讓我逃脫&?我無法重命名服務器上的文件,這會更容易但不可能。

文件存儲在數據庫中,然後檢索並修剪其獨特附件。 :)

$fileName = $eachFile['filename']; 
$fileNamePretty = explode('__', $fileName); // ONLY FILENAME 

,然後在下載鏈接我:

 <a href="../download.php?filename=<?php echo $fileName?>"> 

和的download.php

<?php 
require 'core/init.php'; 
$filename = $_GET['filename']; 
$dir = "training/trainingDocuments/"; 
$downloadFilename = $dir.$filename; 

//8 - SUBSEA QUESTIONS__51f034ab37a8e.xls 
//if the filename exists 
if(is_file($downloadFilename)){ 

//send the headers 
header('Pragma: public'); //FIX IE6 Content-Disposition 
header('Content-Description: File Transfer'); 
header('Content-Transder-Encoding: binary'); 
header(sprintf('Content-Length: %u', filesize($downloadFilename))); 
header('Content-type: application/octet-stream'); 
header("Content-Disposition: attachment; filename=".$downloadFilename.""); 

readfile($downloadFilename); 

$training->addToDownload($filename);//updates download count 

}else{ 
//file doesn't exist 
echo "File no exist\n\n"; 
echo $downloadFilename; 

感謝所有幫助

+0

你試圖逃脫與通過URL編碼的URI? –

回答

3

如果沒記錯的話,&被任何網址編碼函數替換爲%26而被轉義。嘗試使用這樣的:

<a href="../download.php?filename=<?php echo urlencode($fileName); ?>"> 

否則後「&」會被視爲在get方法的第二個參數的一切。

+0

工作完美,謝謝。由於有用的%26,所以將其標記爲答案。我使用了%20來避免w3c錯誤,因爲我認爲這樣做會很有用。謝謝。 – user2608855