在php中上传文件后找不到我的文件


can't find my file after file upload in 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中都没有。

我有没有错过什么?

首先,您需要注意字符串字面量中的反斜杠:

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

应该将它们加倍,以防止意外的特殊转义序列。

第二,你缺少了一个尾斜杠:

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

由于您缺少最后一个反斜杠,我相信您会找到一个名为:

的文件
C:'Users'Maria'Documents'IT-learningtest2.xml

同样,信任用户输入的原样(例如,文件名)也不是很安全。