Skip to content
Advertisement

Processing multi-dimensional $_FILES array

I have the following $_FILES array past through for processing from a support form

Array
(
    [file] => Array
        (
            [name] => Array
                (
                    [0] => Test.jpg
                    [1] => Test.doc
                    [2] => Test.php
                    [3] => 
                )

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

            [tmp_name] => Array
                (
                    [0] => /tmp/phpCO0vSD
                    [1] => /tmp/phpEFpp3Q
                    [2] => /tmp/phpwN4Iwc
                    [3] => 
                )

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

            [size] => Array
                (
                    [0] => 1305787
                    [1] => 339773
                    [2] => 480098
                    [3] => 0
                )

        )

)

My main problem is understanding the logic required in order to process the array, check each file is valid (which I already have a list of valid extensions) then rename and store the file appropriately.

The solutions on Google and SO are both rather complex for my simple requirements.

Advertisement

Answer

Here is how you would traverse your array:

foreach ($_FILES['file']['name'] as $key => $name) {
    echo $name;
    echo $_FILES['file']['type'][$key];
    echo $_FILES['file']['tmp_name'][$key];
}

Having this loop you can do what you want easily.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement