0

这是HTML:

<h1>Upload Custom Logo</h1>
<form action="settings.php" enctype="multipart/form-data" method="post">
<input type="file" name="Logo" />
<input type="submit" value="Upload" name="logoUpload" />
</form>

这是PHP:

<?php /* Handle Logo Upload */
if($_POST['logoUpload']) {

$target_path = "uploads/";
$Logo = $_FILES['Logo']['name'];

$target_path = $target_path . basename( $_FILES['Logo']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo '<div class="statusUpdate">The file '.  basename( $_FILES['Logo']['name']). 
        ' has been uploaded</div>';
    }   

} else{
    echo '<div class="statusUpdate">There was an error uploading the file to: '.$target_path.', please try again!  Error Code was: '.$_FILES['Logo']['error'].'</div>';
}

}
?>

它总是转到该代码的“else”语句,我在与此 php 文件设置为 777 文件权限的同一目录中有一个上传文件夹。我正在测试上传的图像大小小于 10KB。但是 move_uploaded_file() 总是失败,并且除了使用 else 语句生成的自定义错误消息之外,它不会返回任何错误消息。

4

2 回答 2

3

You're referring to the file in the $_FILES array by two different names -
$Logo = $_FILES['Logo']['name'];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)

Which one is it? Logo or uploadedfile ?

You're probably referencing an array index which doesn't exist.

于 2012-04-09T22:28:59.010 回答
1

可能的原因:

  • 文件的上传以某种方式失败。
  • 您无权将文件移动到目标路径。

正如 PHP.NET 所说:

如果 filename 不是有效的上传文件,则不会发生任何操作,并且 move_uploaded_file() 将返回 FALSE。

如果 filename 是有效的上传文件,但由于某种原因无法移动,则不会发生任何操作,并且 move_uploaded_file() 将返回 FALSE。此外,还会发出警告。

参考: http: //php.net/manual/en/function.move-uploaded-file.php

于 2012-04-09T22:27:28.263 回答