Access multidimensional arrays of an input in php

0

I have several image arrays in the following format:

<input type="file" name="imagens[$id][]">

The $id is changed dynamically. How do I access get some of the image data, name for example in php?

Updating:

Array
(
    [45] => Array
        (
            [0] => 1.jpg
            [1] => 2.jpg
            [2] => 
        )

    [44] => Array
        (
            [0] => 4.jpg
            [1] => 5.jpg
            [2] => 
        )

)

Solution: I was able to access it this way:

$_FILES['imagens']['name'][$id]
    
asked by anonymous 01.02.2016 / 18:02

2 answers

0

As you do not know the value of the key, it is best to go through the array:

foreach($_FILES['imagens'] as $img){
   echo $img['tmp_name'];
}
    
01.02.2016 / 18:11
0

First of all, note that in your array not the name or tmp_name key that is common when using $_FILES in PHP.

In this way I assume that you must be using the wrong method to capture the form data. (you should be using $_POST ).

Generally when you want to do a upload of file (s), use:

<form action="upload.php" enctype="multipart/form-data" method="post">
    <input type="file" name="imagens[$id][]">
</form>

Note that form is method="post" , however in PHP you should capture it by $_FILES . What should generate an array like this:

Array
(
    [45] => Array
        (
            [0] => Array 
                (
                    [name] => 1.jpg
                    [type] => image/jpeg
                    [tmp_name] => /tmp/php/php6hst32
                    [size] => 98174
                )
            [1] => Array 
                (
                    [name] => 2.jpg
                    [type] => image/jpeg
                    [tmp_name] => /tmp/php/php6hst32
                    [size] => 98174
                )
        )

    [44] => Array
        (
            [0] =>  Array 
                (
                    [name] => 4.jpg
                    [type] => image/jpeg
                    [tmp_name] => /tmp/php/php6hst32
                    [size] => 98174
                )
        )
)

To manipulate your content you can use foreach :

foreach($_FILES as $id => $files){
    foreach($files as $k => $file){
        $name = $file['name'];
        $tmpName = $file['tmp_name'];

        printf("name : %s\n tmpName : %s", $name, $tmpName);
    }
}
    
02.02.2016 / 13:43