0

How to detect in PHP if uploaded image is empty?

msor
  • 5
  • 5
  • ``$_FILES['file']['size']`` refers to the size of the image in bytes and not the length of the filename. For this you should use ``strlen($_FILES['file']['name'])``. – alistaircol May 24 '17 at 14:39
  • if (isset($_FILES['file'])) ? – Frankich May 24 '17 at 14:39
  • An error code of "4" can also be returned if no file is uploaded. The 2nd condition of your if statement may be false. – ViLar May 24 '17 at 14:42
  • $_FILES['file']['error'] == 0 will return true if the upload was successful. It's more readable to use the constants UPLOAD_ERR_OK, UPLOAD_ERR_INI_SIZE, etc. See http://php.net/manual/en/features.file-upload.errors.php – aljo f May 24 '17 at 14:43
  • Why don't you just inspect the variable? Even if you haven't configured a proper debugger, you can at least `var_dump()` it. – Álvaro González May 24 '17 at 14:44

4 Answers4

1

Using/checking file name values:

It is BAD PRACTISE to use the file name given by the upload script. You have no idea what this value can be or what any user of your script will try and set this value as.

If you want to check that the file name given by the upload form is blank the you can simply use if(empty($_FILES['file']['tmp_name'])){... which will return boolean (true/false). Read about empty().

Syntax Inconsistency:

You have a logical inconsistency here:

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

if the file size is zero this usually implies the file is NULL or empty or otherwise not a file you actually want to have uploaded. BUT $_FILES['file']['error'] will only return zero if there is no error.

What you probably want is error number 4 which specifically means no file was uploaded :

if($_FILES['file']['error'] == 4){
    //no file was uploaded. 
}

See the manual reference.

Example code checking file upload is ok and checking file name is safe:

As I said above, it is BAD PRACTISE to use the name given by the upload script and if you're going to use this value I would strongly suggest you use preg_replace on it to remove invalid characters and then test if empty:

if($_FILES['file']['error'] == 0 && $_FILES['file']['size'] > 0){
    ///file was uploaded ok, has a size above zero bytes. 

    $uploadedFileName = preg_replace("/[^0-9a-z._-]/i","",$_FILES['file']['name']);
    if (empty($uploadedFileName)){

        //sorry no file name was given....

    }
}
Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
0

You can easily check if is empty with

$uploadedFile = $_FILES['file']['name'];
if ($uploadedFile==""){
  //do something
}
SilverSurfer
  • 4,281
  • 6
  • 24
  • 50
0

Check first if the file name is set, afterwards check if the value of the file name is not empty.

Please try the following code:

if (isset($_FILES['file']['name'])) {
    if ($_FILES['file']['name'] != "") {
        $uploadedFile = $_FILES['file']['tmp_name'];
        $image = imagecreatefrompng($uploadedFile);
        ...
    }
}
Julien Ambos
  • 2,010
  • 16
  • 29
0

You can try:

1.

if (isset($_FILES['file']['name'])) {
    //do something
}
  1. use $_FILES['userfile']['error']

see more: http://php.net/manual/en/features.file-upload.errors.php

Alex
  • 3,646
  • 1
  • 28
  • 25