45

on a page, i have :

if (!empty($_FILES['logo']['name'])) {
    $dossier     = 'upload/';
    $fichier     = basename($_FILES['logo']['name']);
    $taille_maxi = 100000;
    $taille      = filesize($_FILES['logo']['tmp_name']);
    $extensions  = array('.png', '.jpg', '.jpeg');
    $extension   = strrchr($_FILES['logo']['name'], '.');

    if(!in_array($extension, $extensions)) {
        $erreur = 'ERROR you  must upload the right type';
    }

    if($taille>$taille_maxi) {
         $erreur = 'too heavy';
    }

    if(!empty($erreur)) {
      // ...
    }
}

The problem is, if the users wants to edit information WITHOUT uploading a LOGO, it raises an error : 'error you must upload the right type'

So, if a user didn't put anything in the inputbox in order to upload it, i don't want to enter in these conditions test.

i tested : if (!empty($_FILES['logo']['name']) and if (isset($_FILES['logo']['name'])

but both doesn't seems to work.

Any ideas?

edit : maybe i wasn't so clear, i don't want to test if he uploaded a logo, i want to test IF he selected a file to upload, because right now, if he doesn't select a file to upload, php raises an error telling he must upload with the right format.

thanks.

sf_tristanb
  • 8,725
  • 17
  • 74
  • 118

7 Answers7

87

You can check this with:

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

Or you can use a javascript construction that only enables the upload/submit button whenever the upload field has a value other then an empty string ("") to avoid submission of the form with no upload at all.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
35

There is a section in php documentation about file handling. You will find that you can check various errors and one of them is

UPLOAD_ERR_OK
    Value: 0; There is no error, the file uploaded with success.
<...>
UPLOAD_ERR_NO_FILE
    Value: 4; No file was uploaded.

In your case you need code like

if ($_FILES['logo']['error'] == UPLOAD_ERR_OK) { ... }

or

if ($_FILES['logo']['error'] != UPLOAD_ERR_NO_FILE) { ... }

You should consider checking (and probably providing appropriate response for a user) for other various errors as well.

Jonas
  • 2,910
  • 2
  • 26
  • 36
  • @Tristan open your eyes and see: Value: 4; **No file was uploaded** – Your Common Sense Jun 02 '10 at 14:29
  • @Col. Shrapnel oooops, indeed, i'm sorry, maybe it's time i put my glasses ;p – sf_tristanb Jun 02 '10 at 14:31
  • 1
    I checked this out for myself. It is indeed the correct way to check for such a case. Also Note: "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." (From the PHP Documentation) http://php.net/manual/en/features.file-upload.post-method.php – Chris Bier Dec 29 '12 at 22:25
14

You should use is_uploaded_file($_FILES['logo']['tmp_name']) to make sure that the file was indeed uploaded through a POST.

baloo
  • 7,635
  • 4
  • 27
  • 35
7

I would test if (file_exists($_FILES['logo']['tmp_name'])) and see if it works.

Or, more approperately (thanks Baloo): if (is_uploaded_file($_FILES['logo']['tmp_name']))

Jess
  • 42,368
  • 6
  • 37
  • 51
2

We Could Use

For Single file:

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

For Multiple files:

if ($_FILES['logo']['tmp_name'][0] == "") {
    // No files were selected for upload, your (re)action goes here
}
Syscall
  • 19,327
  • 10
  • 37
  • 52
Divyanshu
  • 21
  • 1
0
if($_FILES["uploadfile"]["name"]=="") {} 

this can be used No file was selected for upload, your (re)action goes here in if body echo "no file selected";

Harsh Mangalam
  • 1,116
  • 1
  • 10
  • 19
-3
if ($_FILES['logo']['error'] === 0)

is the only right way

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    This is contrary to what the PHP Documentation states. This will actually evaluate to true if the file was uploaded successfully. http://php.net/manual/en/features.file-upload.errors.php – Chris Bier Dec 29 '12 at 22:27