为什么我的php移动文件脚本实际上没有移动文件?

时间:2010-07-30 14:16:27

标签: php html file-upload

我使用php将文件上传到服务器,而move_uploaded_file函数没有返回任何错误,该文件不在目标文件夹中。如您所见,我正在使用root的确切路径,并且上传的文件低于最大大小。

$target = "/data/array1/users/ultimate/public_html/Uploads/2010/";  
//Write the info to the bioHold xml file.
$xml = new DOMDocument();
$xml->load('bioHold.xml');
$xml->formatOutput = true;
$root = $xml->firstChild;
$player = $xml->createElement("player");
$image = $xml->createElement("image");
$image->setAttribute("loc", $target.basename($_FILES['image']['name']));
$player->appendChild($image);
$name = $xml->createElement("name", $_POST['name']);
$player->appendChild($name);
$number = $xml->createElement("number", $_POST['number']);
$player->appendChild($number);
$ghettoYear = $xml->createElement("ghettoYear", $_POST['ghetto']);
$player->appendChild($ghettoYear);
$schoolYear = $xml->createElement("schoolYear", $_POST['school']);
$player->appendChild($schoolYear);
$bio = $xml->createElement("bio", $_POST['bio']);
$player->appendChild($bio);
$root->appendChild($player);
$xml->save("bioHold.xml");
//Save the image to the server.
$target = $target.basename($_FILES['image']['name']);
if(is_uploaded_file($_FILES['image']['tmp_name']))
    echo 'It is a file <br />';
if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
    echo $_FILES['image']['error']."<br />";
}
else {
    echo $_FILES['image']['error']."<br />";
    echo $target;   
}

感谢任何帮助。

Eric R。

3 个答案:

答案 0 :(得分:1)

您需要确保托管您网页的任何人都配置了允许您上传和移动文件的设置。大多数人会禁用这些功能,因为这是一种安全风险。

只需发送电子邮件并询问是否已启用。

希望这有帮助。

答案 1 :(得分:1)

最喜欢这个是权限问题。我将假设您没有任何直接的shell访问权来直接检查这些内容,所以这里是如何从脚本中执行此操作:

检查$target目录是否存在:

$target = '/data/etc....';
if (!is_dir($target)) {
    die("Directory $target is not a directory");
}

检查它是否可写:

if (!is_writable($target)) {
    die("Directory $target is not writeable");
}

检查完整目标文件名是否存在/是否可写 - 可能存在但不能被覆盖:

 $target = $target . basename($_FILES['image']['name']);
 if (!is_writeable($target)) {
     die("File $target isn't writeable");
 }

除此之外:

if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
    echo $_FILES['image']['error']."<br />";
}

在此处回显error参数是没有用的,它纯粹指的是上传过程。如果文件已正确上载但无法移动,则仍会仅显示0(例如UPLOAD_ERR_OK常量)。检查错误的正确方法如下:

if ($_FILES['images']['error'] === UPLOAD_ERR_OK) {
    // file was properly uploaded
    if (!is_uploaded_File(...)) {
        die("Something done goofed - not uploaded file");
    }
    if (!move_uploaded_file(...)) {
        echo "Couldn't move file, possible diagnostic information:"
        print_r(error_get_last());
        die();

    }
} else {
    die("Upload failed with error {$_FILES['images']['error']}");
}

答案 2 :(得分:0)

您对is_uploaded_file和move_uploaded_file的调用各不相同。对于is_uploaded_file,您正在检查'name',而对于您正在传递'tmp_name'的move_uploaded_file。尝试将您对move_uploaded_file的调用更改为使用'name'