2013-04-20 58 views
0

我用下面的PHP腳本上傳文件後:找不到我的檔案在PHP文件上傳

<?php 
$dest_dir="C:\Users\Maria\Documents\IT-learning"; 
foreach ($_FILES as $file_name => $file_array) { 
    echo "path: ".$file_array['tmp_name']."<br/>\n"; //output is "C:\Windows\Temp\phpB4C9.tmp" instead 
    echo "name: ".$file_array['name']."<br/>\n"; 
    echo "type: ".$file_array['type']."<br/>\n"; 
    echo "size: ".$file_array['size']."<br/>\n"; 

    if (is_uploaded_file($file_array['tmp_name'])) { 
     move_uploaded_file($file_array['tmp_name'], $dest_dir.$file_array['name']) 
     or die ("file exists but can't be moved"); 
     echo "File uploaded successfully."; 
    } else { 
     echo "File does not exist."; 
    } 
} //single file is fine. opened single file is 
?> 

輸出是這樣的:

path: C:\Windows\Temp\phpB4C9.tmp 
name: test2.xml 
type: text/xml 
size: 4523 
File uploaded successfully. 

我的問題是我除了在原始目錄中,在我的電腦上看不到test2.xml文件。從我的理解,我應該看到它移動到C:\Users\Maria\Documents\IT-learning。但我沒有看到它在C:\Users\Maria\Documents\IT-learningC:\Windows\Temp\phpB4C9.tmp

我想知道什麼嗎?

+0

你使用了什麼服務器包? – Daedalus 2013-04-20 02:39:42

回答

1

首先,你需要注意你的反斜槓在字符串:

$dest_dir="C:\\Users\\Maria\\Documents\\IT-learning"; 

你應該加倍起來,以防止意外的特殊轉義序列。

其次,你缺少一個斜線:

$dest_dir="C:\\Users\\Maria\\Documents\\IT-learning\\"; 

因爲你缺少的最後一個反斜槓,我相信你會找到一個文件名爲類似:

C:\Users\Maria\Documents\IT-learningtest2.xml 

此外,它是不太現實的信任用戶輸入(例如,文件的名稱)。

+0

非常感謝。是的,它位於C:\ Users \ Maria \ Documents \ IT-learningtest2.xml中。我不明白爲什麼我在搜索「test2」時沒有找到這個文件。最後一個反斜槓是絕對必要的。在這種情況下,雙反斜槓不是必需的。 – 2013-04-20 13:20:25

+1

如果它位於像'n'這樣的字符之前,則必須使用雙斜線,但我更願意將它們全部加倍。 – Matthew 2013-04-20 18:18:59

+0

是的,你是對的。 – 2013-04-21 15:53:33