php 不上传任何内容时,$_FILES 数组不为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12538718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:45:20  来源:igfitidea点击:

$_FILES array is not empty upon uploading nothing

phpfileupload

提问by user1672267

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.

我试图仅在仅当if(!empty($_FILES['image'])){但由于某种原因在不提交文件且仅单击提交时数组不为空时才运行我的代码。

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

    ?>

采纳答案by Oliver Tappin

Use the is_uploaded_filePHP function instead:

改用is_uploaded_filePHP 函数:

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

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

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

回答by The Alpha

This is how $_FILESarray looks like when nothing uploaded

这是未$_FILES上传任何内容时数组的样子

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

So it's never empty.

所以它永远不会是空的。

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

错误代码 4[error] => 4表示没有上传文件,错误code 0表示没有错误并且文件已上传,因此您可以检查

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

Here is another answeron SO.

这是关于 SO 的另一个答案

回答by hakre

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:

您应该首先查看 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.

如果您的表单中没有选择要上传的文件,PHP 将返回$_FILES['userfile']['size']0 和$_FILES['userfile']['tmp_name']无。

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

因此,如果您确实想知道image是否已提交该元素的任何文件,请检查它:

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

回答by Pavnish Yadav

Check value is not null:

检查值不为空:

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

回答by rarenicks

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

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

UPLOAD_ERR_OK如果没有错误,则返回值 0,文件上传成功。

回答by Calin Rusu

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
}
?>

回答by jimp

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

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.

如果表单中没有选择要上传的文件,PHP 将返回 $_FILES['userfile']['size'] 为 0,而 $_FILES['userfile']['tmp_name'] 为 none。

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

即使用户没有选择要上传的文件,您也会为每个“文件”上传字段获得一个数组条目。

回答by Eng Hany Atwa

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

or

或者

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

回答by Badshah Sahib

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