我使用以下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-learning
或C:\Windows\Temp\phpB4C9.tmp
中都没有看到它。
我是否想念任何事情?
答案 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
此外,按原样信任用户输入(例如,文件名称)并不十分安全。