PHP 中的意外计数值

Unexpected Count Value in PHP

我有file uploader

<input type="file" name="photos[]" id="files" multiple="true" />

我需要在上传器中上传的文件总数。

echo count($_FILES['photos']['name']);

我没有应用任何增量。

为什么会这样,我一头雾水...

这应该可以更好地说明为什么会发生这种情况。

当您有(例如)其中两个字段并在不放置任何文件的情况下提交。这就是 $_FILES 的样子:

Array
(
    [photos] => Array
        (
            [name] => Array
                (
                    [0] => 
                    [1] => 
                )

            [type] => Array
                (
                    [0] => 
                    [1] => 
                )

            [tmp_name] => Array
                (
                    [0] => 
                    [1] => 
                )

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

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

        )

)

现在,在提交后,您的计数是 echo count($_FILES['photos']['name']);,等于 2。但是字段是空的。

里面error索引其实是代码,这里解释一下:

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

那里的两个 4 表示:

UPLOAD_ERR_NO_FILE
值:4;没有上传文件。