Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP multiple file upload check empty file

Tags:

html

php

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?

like image 430
Michael Kuan Avatar asked Nov 29 '15 03:11

Michael Kuan


People also ask

What is move_ uploaded_ file in PHP?

Definition and Usage. The move_uploaded_file() function moves an uploaded file to a new destination. Note: This function only works on files uploaded via PHP's HTTP POST upload mechanism. Note: If the destination file already exists, it will be overwritten.

How to get upload file path in PHP?

php $dir = dirname(__FILE__); echo "<p>Full path to this dir: " . $dir . "</p>"; echo "<p>Full path to a . htpasswd file in this dir: " .


1 Answers

use foreach

$i=1
foreach($_FILES['file']['name'] as $file){
if(empty($file))
{
    echo "file $i empty";
    $i++
}
}
like image 193
Abhishek Sharma Avatar answered Oct 04 '22 12:10

Abhishek Sharma