检查特定输入文件是否为空

php
2022-08-30 11:45:08

在我的表单中,我有3个用于文件上传的输入字段:

<input type=file name="cover_image">
<input type=file name="image1">
<input type=file name="image2">

如何检查是否为空 - 没有文件用于上传?cover_image


答案 1

您可以使用数组上的字段进行检查,如下所示:size$_FILES

if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0)
{
    // cover_image is empty (and not an error)
}

(我还在此处检查错误,因为如果出现问题,它可能是0。我不会为此检查使用名称,因为它可以被覆盖)


答案 2

方法 1

if($_FILES['cover_image']['name'] == "") {
// No file was selected for upload, your (re)action goes here
}

方法 2

if($_FILES['cover_image']['size'] == 0) {
// No file was selected for upload, your (re)action goes here
}

推荐