我想在编辑面板中更新图像,我在那里显示了上传的图像名称,如果用户上传,则没有问题代码可以正常工作,但是如果不上传,则图像上传代码仍在工作,并显示无效的文件类型。请告诉我是什么问题。
请建议对代码进行增强
if(isset($_FILES['image']))
{
if($_FILES['image']['type']=="image/jpeg" || $_FILES['image']
['type']=="image/jpg" || $_FILES['image']['type']=="image/png")
{
$imgname=$_FILES['image']['name'];
move_uploaded_file($_FILES['image']
['tmp_name'],"recent_upcoming_img/".$_FILES['image']['name']);
$imgquer="select fimage from $table where date='$date'";
$imgres=mysqli_query($conn,$imgquer);
$r=mysqli_fetch_assoc($imgres);
$preimg="recent_upcoming_img/".$r['fimage'];
unlink($preimg);
$quer="update $table set
title='$title',content='$content',fimage='$imgname' where
date='$date'";
$res=mysqli_query($conn,$quer);
if($res)
{
unset($_SESSION['table_e']);
unset($_SESSION['date']);
header("Location:dashboard.php");
}
else{
header("Location:edit.php");
}
}
else
{
$imgname=$_FILES['image']['name'];
echo '<script language="javascript">';
echo 'if(alert("INCORRECT FILE TYPE ")){} ';
echo 'else{window.location = "edit.php"}';
echo '</script>';
}
}
else
{
$quer="update $table set title='$title',content='$content' where
date='$date'";
$res=mysqli_query($conn,$quer);
if($res)
{
unset($_SESSION['table_e']);
unset($_SESSION['date']);
header("Location:dashboard.php");
}
else{
header("Location:edit.php");
}
}
答案 0 :(得分:0)
即使文件未上传, $ _ FILES 也会返回一个空数组(如下所示)
Array
(
[image] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
按如下所示更改if条件
if(!empty($_FILES['image']['name']))
{
// Write your codes here
}