$_FILES array is not empty upon uploading nothing

Deadly 提交于 2019-12-09 14:40:23

问题


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.";
        }

    ?>


回答1:


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




回答2:


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.




回答3:


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) {
        ...
    }
}



回答4:


Check value is not null:

in_array(!null,$_FILES['field_name']['name'])



回答5:


if($_FILES['image']['error'] === UPLOAD_ERR_OK) {
    // Success code Goes here .. 
}

UPLOAD_ERR_OK returns value 0 if there is no error, the file was uploaded successfully.




回答6:


http://php.net/manual/en/features.file-upload.post-method.php

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.

You get an array entry per "file" upload field, even if the user didn't select a file to upload.




回答7:


I have confronted this issue with a multiple file input. What I found to be working for checking if any file has been selected is:

<?php
$size_sum = array_sum($_FILES['img']['size']);
if ($size_sum > 0) {
 // at least one file has been uploaded
} else {
 // no file has been uploaded
}
?>



回答8:


if(!empty($_FILES['image']['name'][0]) || !empty($_FILES['image']['name'][1]) ||  
!empty($_FILES['image']['name'][2]))

or

for($1=0;$i<count($_FILES['image']);$i++){
if(!empty($_FILES['image']['name'][$i])){
   // do something
}
}



回答9:


if(isset($_FILES['file'])){//do some thing here}


来源:https://stackoverflow.com/questions/12538718/files-array-is-not-empty-upon-uploading-nothing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!