Home » Php » html – PHP multiple file upload check empty file

html – PHP multiple file upload check empty file

Posted by: admin July 12, 2020 Leave a comment

Questions:

Let’s say I have a form like this:

<form action="upload.php" method="post" enctype="multipart/form-data">
File 1 : <input type="file" name="file[]" />
File 2 : <input type="file" name="file[]" />
<input type="submit" name="submit" value="Upload" />
</form>

I want to make sure that each file had upload file.
Here is my condition and the code that I write:

File 1 empty:

if(empty($_FILES['file']['name'][0]))
{
    echo 'file 1 empty';
}

File 2 empty:

if(empty($_FILES['file']['name'][1]))
{
    echo 'file 2 empty';
}

File 1 and File 2 empty:

if(empty($_FILES['file']['name'][0]) && ($_FILES['file']['name'][1]))
{
     echo 'file 1 and file 2 empty';
}

Is it possible to write the above condition in for loop? Or just seperately write the code is enough?

How to&Answers:

use foreach

$i=1
foreach($_FILES['file']['name'] as $file){
if(empty($file))
{
    echo "file $i empty";
    $i++
}
}