PHP-检查一个文件是否已上传,但其余信息为空


PHP - Checking a file has been uploaded but the rest of the information is empty

我有一个表格,要求上传图像、标题、描述等。

如果没有像这样选择图像,则会出现错误:

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(!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
}

在您的代码中,您使用了&&,这意味着只有当所有字段都为空时,错误才会返回。此外,empty($_FILES['target_file'])前面的感叹号意味着,如果文件,则会出现错误,如果没有,则不会出现错误。

此外,没有必要将每个empty(/*post field*/)零件都包含在

的括号中