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


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" />

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

File 1 empty:

    echo 'file 1 empty';

File 2 empty:

    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

foreach($_FILES['file']['name'] as $file){
    echo "file $i empty";