Gvice
Gvice

Reputation: 391

multidimensional for loops in php

I have the following print_r, which is derived from "multiple multiple(not typo) file uploads".

Array
(
[file] => Array
    (
        [name] => Array
            (
                [1] => Array
                    (
                        [0] => IMG_8502 f 5 .jpg
                        [1] => IMG_8507 f 5 .jpg
                    )

                [2] => Array
                    (
                        [0] => IMG_8508 f 5 .jpg
                        [1] => IMG_8529 f 5 .jpg
                        [2] => IMG_8612 fff 5 .jpg
                    )

                [3] => Array
                    (
                        [0] => IMG_8502 f 5 .jpg
                        [1] => IMG_8507 f 5 .jpg
                    )

            )

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

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

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

            )

        [tmp_name] => Array
            (
                [1] => Array
                    (
                        [0] => C:\wamp\tmp\php1CC.tmp
                        [1] => C:\wamp\tmp\php1FC.tmp
                    )

                [2] => Array
                    (
                        [0] => C:\wamp\tmp\php23B.tmp
                        [1] => C:\wamp\tmp\php26B.tmp
                        [2] => C:\wamp\tmp\php2AB.tmp
                    )

                [3] => Array
                    (
                        [0] => C:\wamp\tmp\php2CB.tmp
                        [1] => C:\wamp\tmp\php30A.tmp
                    )

            )

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

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

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

            )

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

                [2] => Array
                    (
                        [0] => 2456713
                        [1] => 2585779
                        [2] => 1770128
                    )

                [3] => Array
                    (
                        [0] => 2776165
                        [1] => 2380025
                    )

            )

    )

)

<?php
if(isset($_FILES['file'])=== true){
$files = $_FILES['file'];
for($x = 0; $x < count($files['name'][$x]); $x++){
    $file_name = $files['name'][$x];
    $tmp_name = $files['tmp_name'][$x];
    move_uploaded_file($tmp_name, 'documents/'.$file_name);
}}?>

I think I need to embed another for loop to access the actual name and tmp arrays... name1, name2, name3. I would like to be able to access each sub array so that I can move them accordingly

Upvotes: 1

Views: 212

Answers (2)

Kerem
Kerem

Reputation: 11586

I use something like this, try it;

function assoc($files) {
    // single image
    if (!is_array($files['name'])) {
        return $files;
    }

    // multiple images
    $assoc = array();
    foreach ($files as $key => $array) {
        foreach ($array as $i => $value) {
            $assoc[$i][$key] = $value;
        }
    }

    return $assoc;
}

echo '<form method="post" action="" enctype="multipart/form-data">
        File 1: <input type="file" name="file[]">
        File 2: <input type="file" name="file[]">
        <input type="submit" name="submit" value="Send">
      </form>';

// and using
if (isset($_FILES['file'])) {
    $files = assoc($_FILES['file']);
    foreach ($files as $i => $file) {
        $move =@ move_uploaded_file($file['tmp_name'], 'documents/'. $file['name']);
        if ($move === true) {
            // remove moved file from array stack
            unset($files[$i]);
        }
    }
}

// display not moved files
print_r($files);

// flash errors
if (!empty($files)) {
    foreach ($files as $file) {
        print $file['name'] ." not uploaded!\n";
    }
}

Outs;

// print_r($_FILES);
// print_r(assoc($_FILES['file']));
Array
(
    [file] => Array
        (
            [name] => Array
                (
                    [0] => Butterfly-wallpaper-butterflies-604274_1024_768.jpg
                    [1] => winter-wallpaper-by_karil.png
                )

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

...

Array
(
    [0] => Array
        (
            [name] => Butterfly-wallpaper-butterflies-604274_1024_768.jpg
            [type] => image/jpeg
            [tmp_name] => C:\Windows\Temp\php4C16.tmp
            [error] => 0
            [size] => 212399
        )

    [1] => Array
        (
            [name] => winter-wallpaper-by_karil.png
            [type] => image/png
            [tmp_name] => C:\Windows\Temp\php4C36.tmp
            [error] => 0
            [size] => 146146
        )

)

Upvotes: 1

Phil
Phil

Reputation: 7106

I think the best way for you to handle this is to refactor the code that generates the Array, so that related fields are put together. Perhaps a structure like:

Array
(
[file] => Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [name] => IMG_8502 f 5 .jpg
                        [type] => image/jpeg
                    )
...

And then you can just iterate through the files:

foreach($files as $upload) {
    foreach($upload as $file) {
        move_uploaded_file($file['tmp_name'], 'documents/'.$file['name']);
    }
}

Upvotes: 1

Related Questions