10

I have this form and I would like to check if the user has selected a file or not.

<form action="upload.php" method="POST" enctype="multipart/form-data" >
    <select name="category">
        <option value="cat1" name="cat1">Productfotografie</option>
        <option value="cat2" name="cat2">Portretten</option>
        <option value="cat3" name="cat3">Achitectuur</option>
    </select>
    <input type="file" name="file_upload">
    <input type="submit" name="submit" value="Upload photo">
</form>

I wrote this PHP code to test it

if (empty($_POST) === false) {
    $fileupload = $_POST['file_upload'];
    if (empty($fileupload) === true) { //
            echo "Error no file selected";     
    } else {
        print_r($_FILES);
    }
} 

But I get the "Error no file selected" even if I DO select something. Any clue? I'm sorry I'm really new in PHP.

EDIT: I already tried replacing $fileupload = $_FILES['file_upload'] but it prints an empty error

(Array ( [file_upload] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) ))

when I do NOT enter a file?

Regolith
  • 2,944
  • 9
  • 33
  • 50
Mae
  • 443
  • 2
  • 5
  • 22
  • 3
    Use [`$_FILES`](http://php.net/manual/en/reserved.variables.files.php) instead of `$_POST` to access uploaded files. – showdev Oct 17 '14 at 19:53
  • @showdev I already tried replacing $fileupload = $_FILES['file_upload'] but it prints an empty error (Array ( [file_upload] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )) when I do NOT enter a file? – Mae Oct 17 '14 at 20:06
  • That's correct: `If no file is selected for upload in your form, PHP will return $_FILES['userfile']['size'] as 0, and $_FILES['userfile']['tmp_name'] as none.` -[php.net](http://php.net/manual/en/features.file-upload.post-method.php) – showdev Oct 17 '14 at 20:09
  • Are you sure that your form enctype is multipart/form-data. if it is not set then it will show empty – Ratheesh PB Feb 04 '16 at 12:21
  • 1
    Does this answer your question? [How to check whether the user uploaded a file in PHP?](https://stackoverflow.com/questions/946418/how-to-check-whether-the-user-uploaded-a-file-in-php) –  Dec 16 '20 at 07:15

3 Answers3

24

Use the $_FILES array and the UPLOAD_ERR_NO_FILE constant:

if(!isset($_FILES['file_upload']) || $_FILES['file_upload']['error'] == UPLOAD_ERR_NO_FILE) {
    echo "Error no file selected"; 
} else {
    print_r($_FILES);
}

You can also check UPLOAD_ERR_OK which indicates if the file was successfully uploaded (present and no errors).

Note: you cannot use empty() on the $_FILES['file_upoad'] array, because even if no file is uploaded, the array is still populated and the error element is set, which means empty() will return false.

MrCode
  • 63,975
  • 10
  • 90
  • 112
  • Thanks, I already tried what the other people suggested before posting it and they are wrong. It does not work. But your code works! Thank you very much. I'm new and still learning, also thank you for the tip about empty() :) You're amazing. – Mae Oct 17 '14 at 20:08
  • what if file type is multiple – Harish Kumar Jun 05 '15 at 06:26
  • Checking using Empty is more reliable `if(!empty($_FILES['file_upload']))` or `if(empty($_FILES['file_upload']))` Really thanks MrCode..!! – Ajmal PraveeN Jul 17 '18 at 07:23
0

First, click submit without choose file

  /* check input 'submit' */        
  if (isset($_POST['submit'])) {
    print($_FILES);
  }

Result:

> Array (
>     [file_upload] => Array
>         (
>             [name] => 
>             [type] => 
>             [tmp_name] => 
>             [error] => 4
>             [size] => 0
>         )
> 
> )

There are array of [file_upload][error] = 4, NOT null or whitespace.
Code value = 4 , meaning UPLOAD_ERR_NO_FILE.
If success and no error, code value = 0.
Check here
So use function isset, not empty

  /* check input submit */
  if (isset($_POST['submit'])) {

    /* check input file_upload on error */
    if (isset($_FILES['file_upload']['error'])) {

      /* check code error = 4 for validate 'no file' */         
      if ($_FILES['file_upload']['error'] == 4) {

        echo "Please select an image file ..";

      }

    }

  }
antelove
  • 3,216
  • 26
  • 20
-2

According to http://www.php.net there is $_FILES available since php 4.1.0. This variable keeps information about uploaded files. So you should code

$fileupload = $_FILES['file_upload'];
if (isset($fileupload)) {
    print_r($_FILES);
} else {
    echo "Error no file selected";     
}
Matthias
  • 3,458
  • 4
  • 27
  • 46
  • 1
    -1 empty() will return always return false, even if no file uploaded. – MrCode Oct 17 '14 at 20:00
  • I tried your code already, and it did not work. If I do not enter a file it just prints an empty array. "Array ( [file_upload] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )" – Mae Oct 17 '14 at 20:05