count($ _ FILES ['file'] ['tmp_name'])计算输入而不是上传的文件

时间:2016-01-03 00:13:50

标签: php

我有很多文件输入,我想查看一次上传的文件数量:

<form id="propForm" class="option" name="imform" action="<?php echo $action[$option]; ?>" method="POST" enctype="multipart/form-data">
    <input type="file" name="file[]" accept="image/jpeg" />
    <input value="<?php echo $op[$option]; ?>" type="submit" name="submitIT">
</form>

在php文件中,我使用count()检查它:

$file_count = count($_FILES['file']['tmp_name']);

if ( $file_count > 0 && isset($_POST['submitIT']) ) {
    echo $file_count;
} else header('Location: /');

如果我在没有上传文件的情况下提交,则回显打印6(输入数量)。

这怎么可能?

修改:$_FILE内容:

Array (
    [file] => Array (
        [name] => Array (
            [0] => 
        )
        [type] => Array (
            [0] => 
        )
        [tmp_name] => Array (
            [0] => 
        )
        [error] => Array (
            [0] => 4
            [1] => 4
            [2] => 4
            [3] => 4
            [4] => 4
            [5] => 4
        )
        [size] => Array (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
        )
    )
)

1 个答案:

答案 0 :(得分:1)

是的,那就是当输入与括号name="file[]"具有相同名称时$ _FILES的工作原理。 例如,有3个输入且只选择了一个文件:

Array
(
    [file] => Array
    (
        [name] => Array
            (
                [0] => 
                [1] => leon-2.jpg
                [2] => 
            )

        [type] => Array
            (
                [0] => 
                [1] => image/jpeg
                [2] => 
            )

        [tmp_name] => Array
            (
                [0] => 
                [1] => C:\xampp\tmp\php447.tmp
                [2] => 
            )

        [error] => Array
            (
                [0] => 4
                [1] => 0
                [2] => 4
            )

        [size] => Array
            (
                [0] => 0
                [1] => 81945
                [2] => 0
            )

        )

    )

您可以使用以下内容检查已上传的文件数量:

$count = 0;
foreach ($_FILES['file']['tmp_name'] as $tmp_name) {
    if (is_uploaded_file($tmp_name)) {
        $count++;
    }
}

if ($count > 0) { 
  // do something... 
}