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

时间:2013-04-20 02:32:55

标签: php html windows

我使用以下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中都没有看到它。

我是否想念任何事情?

1 个答案:

答案 0 :(得分:1)

首先,你需要小心字符串文字中的反斜杠:

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

你应该加倍它们以防止意外的特殊逃脱序列。

其次,你错过了一个尾部斜杠:

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

由于您错过了最后一个反斜杠,我相信您会找到一个名为的文件:

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

此外,按原样信任用户输入(例如,文件名称)并不十分安全。