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

93

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.

3
  • 24
    An error code of "4" can be returned if no file is uploaded. Here is the PHP Reference
    – Bentleyo
    Nov 24, 2014 at 23:38
  • I use it like... if ($_FILES['cover_image']['name'] == true){ ... }
    – Sandhu
    Feb 6, 2016 at 8:05
  • @Sandhu that's not even remotely valid, and is not what I suggested. Feb 9, 2016 at 8:00
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
  • 1
    Appropriate method to me. :)
    – Touhid
    Dec 24, 2014 at 18:31
  • Instead of if($_FILES['cover_image']['name'] == "") { you can use if(empty($_FILES['cover_image']['name'])) {.
    – dnns
    Dec 27, 2022 at 5:00
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...
}
0
7
if (empty($_FILES['cover_image']['name']))
1
  • 1
    That doesn't check the validaty of the file. Better to use is_uploaded_file in combination.
    – BenM
    Jan 22, 2013 at 12:31
5

simple :

if($_FILES['cover_image']['error'] > 0)
    // cover_image is empty
2
  • Empty File means Error 4. Above fails.
    – WhiteHorse
    Jun 20, 2018 at 18:14
  • @WhiteHorse that's right, if the error code is more than 0, the upload not worked Jul 30, 2018 at 17:43
3

check after the form is posted the following

$_FILES["cover_image"]["size"]==0
3
    if (!$_FILES['image']['size'][0] == 0){ //}
0
2
 if( ($_POST) && (!empty($_POST['cover_image'])) )    //verifies  if post exists and cover_image is not empty
    {
    //execute whatever code you want
    }
2
  • I did this one first and then check for empty on $_FILES, otherwise the non existent $_FILES threw an error.
    – Wayfarer
    Jun 10, 2017 at 13:56
  • This does not work with <input type=file>. Use $_FILES instead and moreover !empty($_FILES["cover_image"]) is not viable.
    – user31782
    Jan 27, 2022 at 8:48
2
if(!empty($_FILES)) { // code if not uploaded } else { // code if uploaded }
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.

1
if($_FILES['img_name']['name']!=""){
   echo "File Present";
}else{
  echo "Empty file";
}
1
if ($_FILES['cover_image']['size'] == 0 && $_FILES['cover_image']['error'] == 0)
{ 
      // Code comes here
}

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

1
<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);
          }
          
        }
1
if ($_FILES['cover_image']['error'] == 4){
    // the user did not choose any file
}else{
   // the user chose a file to be uploaded
}
1

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

}
0

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.