2013-02-11 87 views
-1

我有一個網站,允許用戶上傳文件。當在另一個頁面上載文件時,用戶可以下載該文件。問題是如果文件名中有一個空格,則只會選取第一個作業而不是整個文件名。我想知道是否有一種方法可以下載帶有空格的文件,或者一個簡單的選項可能是將下劃線添加到文件名以便下載。用Underscore替換空格,以便我可以下載文件。

//This is the directory where images will be saved 
$target = "../images/avatars/"; 
$target = $target . basename($_FILES['photoLocation']['name']); 

// Check to see all fields have been completed 
$photoLocation =($_FILES['photoLocation']['name']); 

if (!empty($photoLocation)) 
{ 

    // Create an SQL query to add the comment 
    $sql = "INSERT INTO tblPhotoUpload (photoLocation) VALUES ('$photoLocation')"; 



    // Connect to the database 
    connect(); 

    // Run the query and store the result in a variable 
    $result = mysql_query($sql) or die("Could not run query"); 


    //Writes the photo to the server 
    if(move_uploaded_file($_FILES['photoLocation']['tmp_name'], $target)) 

    // Close connection to the database 
    mysql_close() 

而我正在顯示這樣的文件。

while($record = mysql_fetch_array($result)) 
{ 
?> 

<tr class="row"> 
    <td class="cell"><a href= http://xxxxxxxxxxxxxxxxxxxxxx.com/images/avatars/<?php   echo $record["photoLocation"];?>>Download</a> </td> 
+0

感謝大家在這麼快的時間回覆。我已經找到了答案,我已經打勾了。 很多你說過一些很棒的東西給我考慮。 謝謝你們 – user1775454 2013-02-11 13:42:54

回答

-2

str_replace()

$文件= str_replace函數(」」, '_',$文件);

或在您的情況:

$target = $target . basename(str_replace(' ','_',$_FILES['photoLocation']['name'])); 

$photoLocation =(str_replace(' ','_',$_FILES['photoLocation']['name'])); 
+0

感謝您的回覆,我的代碼應該放在哪裏? – user1775454 2013-02-11 13:28:26

+0

看到編輯答案 – vodich 2013-02-11 13:35:47

+0

接受答案和-1 :) – vodich 2013-02-12 07:14:10

2

你正在處理一個轉義方案的逃脫方案中,當你使用網址。特別是,你有逃跑的網址,以便一個URL,然後你要逃避URL作爲對HTML嵌入它:

$url = "http://somesite.tld/some/path/" . urlencode($filename); 


echo '<a href="' . htmlspecialchars($url) . '">link</a>'; 

這實在是太該死很難想象的(現實的)情況這實際上需要htmlspecialchars,但wooo偏執狂!

哦,而且,繼續前進並引用所有屬性值通常是個好主意。

當你這樣做:<tag attr=value with spaces>瀏覽器解釋withspaces作爲附加屬性,attr


如果你這樣做,雖然走的路線替代的價值NOTAS一部分,你只是在尋找一個簡單的str_replace函數電話:

$val = str_replace(' ', '_', $replaceMe); 
+0

感謝您的答覆,替換方法會很好,只是在我的代碼中約需要我嗎? – user1775454 2013-02-11 13:29:23

+0

@ user1775454無論何時您處理文件名:所以當保存上傳然後重新下載它時。 – Corbin 2013-02-11 13:32:45

0

還沒有測試,但這種邏輯似乎是正確的。

$name = str_replace(' ', '_', $photoLocation); 
0

我會建議不要顯示圖像的原始名稱,因爲有很多與此相關的安全問題。

你可以這樣做的一種方式是編碼文件名並將其保存在數據庫的額外字段中。

例如

$sql = "INSERT INTO tblPhotoUpload (photoLocation,photourl) VALUES ('$photoLocation','".md5($photoLocation)."')"; 

當顯示器使用phtolocation的URL並顯示路由到照片的位置。

祝你好運,如果你需要完整的例子讓我知道