48

In my form I have 3 input fields for file upload:

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

How can I check if cover_image is empty - no file is put for upload?

16 Answers 16

94

You can check by using the size field on the $_FILES array like so:

if ($_FILES['cover_image']['error'] == 4 || ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0))
{
    // cover_image is empty (and not an error), or no file was uploaded
}

(I also check error here because it may be 0 if something went wrong (ie. a file was selected, but there's no data received). I wouldn't use name for this check since that can be overridden). error with a value of 4 is UPLOAD_ERR_NO_FILE, so we can check for that too.

Sign up to request clarification or add additional context in comments.

3 Comments

An error code of "4" can be returned if no file is uploaded. Here is the PHP Reference
I use it like... if ($_FILES['cover_image']['name'] == true){ ... }
@Sandhu that's not even remotely valid, and is not what I suggested.
32

Method 1

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

Method 2

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

2 Comments

Appropriate method to me. :)
Instead of if($_FILES['cover_image']['name'] == "") { you can use if(empty($_FILES['cover_image']['name'])) {.
13

You can check if there is a value, and if the image is valid by doing the following:

if(empty($_FILES['cover_image']['tmp_name']) || !is_uploaded_file($_FILES['cover_image']['tmp_name']))
{
   // Handle no image here...
}

Comments

7
if (empty($_FILES['cover_image']['name']))

1 Comment

That doesn't check the validaty of the file. Better to use is_uploaded_file in combination.
5

simple :

if($_FILES['cover_image']['error'] > 0)
    // cover_image is empty

2 Comments

Empty File means Error 4. Above fails.
@WhiteHorse that's right, if the error code is more than 0, the upload not worked
3

check after the form is posted the following

$_FILES["cover_image"]["size"]==0

Comments

3
 if( ($_POST) && (!empty($_POST['cover_image'])) )    //verifies  if post exists and cover_image is not empty
    {
    //execute whatever code you want
    }

2 Comments

I did this one first and then check for empty on $_FILES, otherwise the non existent $_FILES threw an error.
This does not work with <input type=file>. Use $_FILES instead and moreover !empty($_FILES["cover_image"]) is not viable.
3
    if (!$_FILES['image']['size'][0] == 0){ //}

Comments

2
if($_FILES['img_name']['name']!=""){
   echo "File Present";
}else{
  echo "Empty file";
}

Comments

2
if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0)
{ 
      // Code comes here
}

This thing works for me........

Comments

2
if(!empty($_FILES)) { // code if not uploaded } else { // code if uploaded }

Comments

2
<input type="file" class="custom-file-input" id="imagefile" name="imagefile[]"  multiple lang="en">
<input type="hidden" name="hidden_imagefile[]" value="<?=$row[2]; ?>" class="form-control border-input" >

    if($_FILES['imagefile']['name'] == '')
        {
          $img = $_POST['hidden_imagefile'];
        }
        else{
          $img = '';
          $uploadFolder = 'uploads/gallery/';
          foreach ($_FILES['imagefile']['tmp_name'] as $key => $image) {
            $imageTmpName = time() .$_FILES['imagefile']['tmp_name'][$key];
            $imageName = time() .$_FILES['imagefile']['name'][$key];
            $img .= $imageName.',';
            $result = move_uploaded_file($imageTmpName, $uploadFolder.$img);
          }
          
        }

Comments

2

$_FILES is an associative POST method array, if You want to check anything about $_FILES You must take into account the index... I tried a lot of suggested options, and the only method that worked for me, was when I included an index in my verification method.

$_FILES['Your_File']['name'][0];

So bye doing this:

 if(empty($_FILES['Your_File']['name'][0])){
    print('this thing is empty');
 }else{
    print('Something, something, something');
 }

There's nothing like good old experimentation and lots of reading.

Comments

2
if ($_FILES['cover_image']['error'] == 4){
    // the user did not choose any file
}else{
   // the user chose a file to be uploaded
}

Comments

2

This will work

if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0)

// checking if file is selected and not an error

{ 
      
// file is not selected and it is not an error

}

Comments

1

UPDATED: Use this method:

First check if 'cover_image' key exists in $_FILES then check other file errors

if (in_array('cover_image', array_keys($_FILES) && $_FILES['cover_image']['error'] == 0) {
  // TODO: write your code
} else {
  // return error
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.