【发布时间】:2012-03-08 22:16:00
【问题描述】:
我有这个表格:
<form action="image_upload.php" method="post" enctype="multipart/form-data">
Image 1: <input type="file" name="event_image" />
<input type="submit" />
</form>
还有这个php代码(image_upload.php):
print_r($_FILES);
if ((($_FILES["event_image"]["type"] == "image/jpeg")
|| ($_FILES["event_image"]["type"] == "image/pjpeg"))
&& ($_FILES["event_image"]["size"] < 200000))
{
if ($_FILES["event_image"]["error"] > 0)
{
echo "Return Code: " . $_FILES["event_image"]["error"] . "<br />";
}
else
{
if (file_exists("/images/events/" . $_FILES["event_image"]["name"]))
{
echo $_FILES["event_image"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["event_image"]["tmp_name"],
"/images/events/" . $_FILES["event_image"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["event_image"]["name"];
}
}
}
else
{
echo "Invalid file";
}
我不知道这是哪里出了问题,因为我以前也有过相同的代码。
我收到以下错误...
Array ([event_image] => Array ([name] => my_image.jpg [type] => image/jpeg [tmp_name] => /private/var/tmp/phpvIYmAZ [error] => 0 [size] => 48512 ) )
警告:move_uploaded_file(../../../images/events/my_image.jpg):无法打开流:第 25 行 /path/event_upload.php 中的权限被拒绝
警告:move_uploaded_file():无法在线将“/private/var/tmp/phpvIYmAZ”移动到/path/event_upload.php 中的“../../../images/events/my_image.jpg” 25 存储在:upload/my_image.jpg
注意:未定义索引:第 57 行 /path/event_upload.php 中的 event_image
【问题讨论】:
-
print_r($_FILES)请在它的顶部。此外,在if上,您应该添加条件isset($_FILES['event_image'])作为要评估的第一个条件。如果这是错误的,你不应该评估其他任何东西,否则你会得到未定义的索引错误。 -
图片在
$_FILES中不存在,可能是因为一开始上传失败。检查是否设置了$_POST['event_image']并验证您没有超过max_upload_size或post_max_size
标签: php image-upload