Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

php - Weird format of $_FILES array when having multiple fields

I have a form that has some fields like this:

<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />

I would expect i would do something like this:

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
                )
        )
)

But i get something like this:

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
                )

        )

)

How do I get the array in the form I expect?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is completely normal format, it always has been like that. If you want a different structure, you can transform it in your application, like this:

<?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]
    );
}

?>

Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...