&引用;未定义索引“;使用php上载图像时
我有以下表格:&引用;未定义索引“;使用php上载图像时,php,image-upload,Php,Image Upload,我有以下表格: <form action="image_upload.php" method="post" enctype="multipart/form-data"> Image 1: <input type="file" name="event_image" /> <input type="submit" /> </form> 图1: 下面是php代码(image_upload.php): print\r($\u文件); 如
<form action="image_upload.php" method="post" enctype="multipart/form-data">
Image 1: <input type="file" name="event_image" />
<input type="submit" />
</form>
图1:
下面是php代码(image_upload.php):
print\r($\u文件);
如果(($_文件[“事件\u图像”][“类型”]=“图像/jpeg”)
||($\u文件[“事件\u图像”][“类型”]=“图像/pjpeg”))
&&($_文件[“事件图像”][“大小”]<200000))
{
如果($\u文件[“事件\u图像”][“错误”]>0)
{
回显“返回代码:”.$\u文件[“事件图像”][“错误”]。“
”;
}
其他的
{
如果(文件存在(“/images/events/”$\u文件[“event\u image”][“name”]))
{
echo$\u文件[“事件图像”][“名称”]”已存在。“;
}
其他的
{
移动上传的文件($文件[“事件图像”][“tmp文件名称”],
“/images/events/”$\u文件[“event\u image”][“name”]);
echo“存储在:“.”upload/“$”文件[“事件图像”][“名称”];
}
}
}
其他的
{
回显“无效文件”;
}
我不知道这是哪里出了问题,因为我以前也用过同样的代码
我得到以下错误,虽然
数组([event_image]=>Array([name]=>my_image.jpg[type]=>image/jpeg[tmp_name]=>/private/var/tmp/phpvIYmAZ[error]=>0[size]=>48512))
警告:move_upload_文件(../../../images/events/my_image.jpg):无法打开流:第25行/path/event_upload.php中的权限被拒绝
警告:move_upload_file():无法将第25行中的“/private/var/tmp/phpvIYmAZ”移动到“../../../../images/events/my_image.jpg”,存储在:upload/my_image.jpg中
注意:未定义索引:第57行/path/event\u upload.php中的event\u image每个属性键下的
$\u文件
数组都以数字形式索引,以处理多个上载
您必须使用$\u文件[“event\u image”][“type”][0]
,$\u文件[“event\u image”][“name”][0]
等硬编码索引零。您必须在不使用数字索引的情况下更改使用$\u文件的每一行
注意:编辑原始答案以修复数值索引在数组中的正确位置。为了让您知道$\u文件对象的外观,请在第8行之前插入此echo“”。打印\u r($\u文件,true)。"";代码>确定。。。这似乎是我本地计算机上的文件夹权限问题。。。只需将文件夹chmod
谢谢你的建议。请在顶部打印($\u文件)
。另外,在if
上,您应该添加条件isset($\u FILES['event\u image'])
作为要计算的第一个条件。如果该值为false,则不应计算任何其他值,否则将出现未定义的索引错误。$\u文件中不存在该图像
,可能是因为它首先未能上载。检查是否设置了$\u POST['event\u image']
,并验证您没有超过max\u upload\u size
或POST\u max\u size
这是错误的$_FILES是一个关联数组,名称字段映射为key:你说得对,我搞砸了!多个上载的数字索引位于该数组的深处。考虑到正确的$_文件结构,对答案进行了编辑。请详细说明您对文件夹/文件权限做了哪些操作以删除错误?在我的例子中,我使用了Dreamweaver。我在“文件”窗口中右键单击该目录,并选择“设置权限”,在那里我可以将权限更改为我需要的权限。
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";
}