PHP-检查已上载的文件,但其余信息为空
我有一个表格,要求上传图片,标题,描述等 如果没有选择像这样的图像,则会出现错误:PHP-检查已上载的文件,但其余信息为空,php,Php,我有一个表格,要求上传图片,标题,描述等 如果没有选择像这样的图像,则会出现错误: if ($_FILES["target_file"]==''){ echo 'Please upload a suitable photograph!<br>'; } if (!empty($_FILES['target_file']) && (empty ($_POST['photo-title'])) && (empty ($_POST['photo-name
if ($_FILES["target_file"]==''){
echo 'Please upload a suitable photograph!<br>';
}
if (!empty($_FILES['target_file']) && (empty ($_POST['photo-title'])) && (empty ($_POST['photo-name'])) && (empty ($_POST['uploaded'])) && (empty ($_POST['genreID'])) && (empty ($_POST['description'])))
{
echo 'Please select a photograph!<br>';
}
if($\u文件[“目标文件”]=''){
echo“请上传一张合适的照片!”;
}
但是,如果我选择了一个图像,但没有填写表单的其余部分,它会将其上载到我的数据库
我试过这样的方法:
if ($_FILES["target_file"]==''){
echo 'Please upload a suitable photograph!<br>';
}
if (!empty($_FILES['target_file']) && (empty ($_POST['photo-title'])) && (empty ($_POST['photo-name'])) && (empty ($_POST['uploaded'])) && (empty ($_POST['genreID'])) && (empty ($_POST['description'])))
{
echo 'Please select a photograph!<br>';
}
if(!empty($_FILES['target_file'])&&(empty($_POST['photo-title'])&&(empty($_POST['photo-name'])&(empty($_POST['upload'])&(empty($_POST['genreID'])&(empty($_POST['description']))
{
echo“请选择一张照片!
”;
}
但这不起作用
有人知道如何在选择图像但不填写表单的其余部分后使其显示错误吗?试试以下方法:
if(!isset($_FILES['target_file']) || !is_uploaded_file($_FILES['target_file']['tmp_name'])){
echo 'Please upload an image!<br>';
} else if(empty($_POST['photo-title']) || empty($_POST['photo-name']) || empty($_POST['uploaded']) || empty($_POST['genreID']) || empty($_POST['description'])){
echo 'Please fill in all the input fields!<br>';
} else {
//upload everything
}
if(!isset($_FILES['target_file'])| |!是否上载了_file($_FILES['target_file']['tmp_name'])){
echo“请上传图像!
”;
}else if(空($_POST['photo-title'])空($_POST['photo-name']))|空($_POST['upload']))|空($_POST['genreID']))|空($_POST['description'])){
echo“请填写所有输入字段!
”;
}否则{
//上传一切
}
在代码中,您使用了&&
,这意味着只有当所有字段都为空时,错误才会回显。另外,empty($\u FILES['target\u file'])
前面的感叹号意味着如果有文件,则会出现错误;如果没有,则不会出现错误
此外,没有必要将每个
空(/*post field*/)
部分括在括号中您的表单是否使用enctype=“多部分/表单数据”
?是所有字段都是文本框/textarea还是有收音机或复选框?在@PinuTried this@Jonan表单中有一个下拉菜单,它似乎工作不正常。即使选择了图像,它也会显示错误“请上载图像!”。即使填写了所有区域,它仍然会显示“请上载图像!”@Jonan@jordan它与一起工作吗!isset()
而不是empty()
?你能告诉我们上传文件时的内容吗?@Jordan把var\u dump($\u FILES)
放在页面顶部时你会得到什么?这是我选择图像时得到的:array(1){[“uploaded”]=>array(5){[“name”=>string(10)“Desert.jpg”[“type”]=>string(10)“image/jpeg”[“tmp_name”]=>string(32)“D:\HND\UniServer\tmp\php67B1.tmp”[“error”]=>int(0)[“size”]=>int(845941)}请上传一个图像!