php – 具有多个字段时$_FILES数组的奇怪格式

前端之家收集整理的这篇文章主要介绍了php – 具有多个字段时$_FILES数组的奇怪格式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的表单有一些像这样的字段:
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />

我希望我会做这样的事情:

Array
(
    [images] => Array
        (
            [0] => Array
                (
                    [name] => test.jpg
                    [type] => image/jpeg
                    [tmp_name] => /tmp/nsl54Gs
                    [error] => 0
                    [size] => 1715
                )

            [1] => Array
                (
                    [name] => test.jpg
                    [type] => image/jpeg
                    [tmp_name] => /tmp/nsl54Gs
                    [error] => 0
                    [size] => 1715
                )

            [2] => Array
                (
                    [name] => test.jpg
                    [type] => image/jpeg
                    [tmp_name] => /tmp/nsl54Gs
                    [error] => 0
                    [size] => 1715
                )

            [3] => Array
                (
                    [name] => test.jpg
                    [type] => image/jpeg
                    [tmp_name] => /tmp/nsl54Gs
                    [error] => 0
                    [size] => 1715
                )
        )
)

但我得到这样的东西:

Array
(
    [images] => Array
        (
            [name] => Array
                (
                    [0] => test.jpg
                    [1] => test.jpg
                    [2] => test.jpg
                    [3] => test.jpg
                )

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

            [tmp_name] => Array
                (
                    [0] => /tmp/nsl54Gs
                    [1] => /tmp/nsl54Gs
                    [2] => /tmp/nsl54Gs
                    [3] => /tmp/nsl54Gs
                )

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

            [size] => Array
                (
                    [0] => 1715
                    [1] => 1715
                    [2] => 1715
                    [3] => 1715
                )

        )

)

如何以我期望的形式获得阵列?

这是完全正常的格式,它总是这样.如果您需要不同的结构,可以在应用程序中对其进行转换,如下所示:
<?PHP

$another_format = array();
for ($i=0; $i<count($_FILES['images']['name']); $i++){
    $another_format[$i] = array(
        'name' => $_FILES['images']['name'][$i],'type' => $_FILES['images']['type'][$i],'tmp_name' => $_FILES['images']['tmp_name'][$i],'error' => $_FILES['images']['error'][$i],'size' => $_FILES['images']['size'][$i]
    );
}

?>

祝你好运!

原文链接:https://www.f2er.com/php/138707.html

猜你在找的PHP相关文章