Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_FILES array is not empty upon uploading nothing

Tags:

file

php

upload

Here is the form

form action="index.php" method="POST" enctype="multipart/form-data" >
        <input type="file" name="image[]" multiple="multiple">
        <input type="submit" value="upload">
    </form> 

I am trying to only run my code when only when if(!empty($_FILES['image'])){ but for some reason the array is not empty upon submitting no files and only clicking submit.

Here is the rest of the code if that will help, thanks.

<html>

Image Upload

    <form action="index.php" method="POST" enctype="multipart/form-data" >
        <input type="file" name="image[]" multiple="multiple">
        <input type="submit" value="upload">
    </form> 

    <?php

    include 'connect.php';

    if(!empty($_FILES['image'])){
    echo $_FILES['image']['error'];
        $allowed = array('jpg', 'gif', 'png', 'jpeg');
        $count = 0;

        foreach($_FILES['image']['name'] as $key => $name){
        $image_name = $name;
        $tmp = explode('.', $image_name);
        $image_extn = strtolower(end($tmp)); //can only reference file
        $image_temp = $_FILES['image']['tmp_name'][$count];
        $filesize = filesize($_FILES['image']['tmp_name'][$count]);
        $count = $count +1;

        if(count($_FILES['image']['tmp_name']) > 5){
            echo "You can upload a maximum of five files.";
            break;
        }

        else if(in_array($image_extn, $allowed) === false){
            echo $name." is not an allowed file type<p></p>";
        }

        else if($filesize > 1024*1024*0.3){
            echo $name." is too big, can be a maximum of 3MB";
        }

        else{

            $image_path = 'images/' . substr(md5($name), 0, 10) . '.' . $image_extn;


            move_uploaded_file($image_temp, $image_path);

            mysql_query("INSERT INTO store VALUES ('', '$image_name', '$image_path')") or die(mysql_error());

            $lastid = mysql_insert_id();
            $image_link = mysql_query("SELECT * FROM store WHERE id = $lastid");
            $image_link = mysql_fetch_assoc($image_link);
            $image_link = $image_link['image'];
            echo "Image uploaded.<p></p> Your image: <p></p><a href = $image_link>$image_path</a>";
            }

        }
        }

    else{
            echo "Please select an image.";
        }

    ?>

like image 894
user1672267 Avatar asked Sep 21 '12 21:09

user1672267


4 Answers

This is how $_FILES array looks like when nothing uploaded

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

So it's never empty.

The error code 4 [error] => 4 indicates no file was uploaded and error code 0 indicates no error and file was uploaded so you can check

if($_FILES['image']['error']==0) {
    // file uploaded, process code here
}

Here is another answer on SO.

like image 152
The Alpha Avatar answered Nov 02 '22 13:11

The Alpha


Use the is_uploaded_file PHP function instead:

if(is_uploaded_file($_FILES['image']['tmp_name'])) {
  //code here
}

http://php.net/manual/en/function.is-uploaded-file.php

like image 8
Oliver Tappin Avatar answered Nov 02 '22 13:11

Oliver Tappin


You should first of all take a look into the PHP manual because - you're not the first one with that problem - the solution has been written in there:

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.

So if you actually want to find out if any file for the image element has has been submitted, check for it:

 $noFile = $_FILES['image']['size'][0] === 0 
           && $_FILES['image']['tmp_name'][0] === '';

Yes, that simple it is.

The test you used:

empty($_FILE);

will only tell you if the whole form has been submitted or not. So an example in full:

$submitted = empty($_FILE);
if ($submitted) {
    $noFile    = $_FILES['image']['size'][0] === 0 
                 && $_FILES['image']['tmp_name'][0] === '';
    if ($noFile) {
        ...
    }
}
like image 4
hakre Avatar answered Nov 02 '22 13:11

hakre


Check value is not null:

in_array(!null,$_FILES['field_name']['name'])
like image 2
Pavnish Yadav Avatar answered Nov 02 '22 13:11

Pavnish Yadav