0

I have this form to upload multiple images to my gallery:

HTML code

<form method="POST" enctype="multipart/form-data" action="gallery.php?action=upload&folder=1">
    <input type="file" name="images[]" multiple />
    <input type="submit" />
</form>

PHP code

$upl_count = count($_FILES['images']);
echo "Uploaded $upl_count images.";

No matter what I upload, $upl_count is always 5, and if I iterate over the files the following way:

PHP code

$images = $_FILES['images'];

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

... the type is always empty. Nothing is echoed in the latter part. What am I missing here? Could it be an encoding problem?

John Weisz
  • 30,137
  • 13
  • 89
  • 132
  • What returns `var_dump($_FILES)`? – Max Oct 25 '14 at 15:09
  • 1
    Recommended reading: [Uploading multiple files](http://php.net/manual/en/features.file-upload.multiple.php) - take especially care how to iterate those multiple files, that is where exactly the numerical index is placed (hint! hint!). - Also: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) - that is enable error logging to the highest level, you will get undefined index warnings in your code or similar. – hakre Oct 25 '14 at 15:11

1 Answers1

0

I've found a solution for your problem here.

So, $_FILES always consists

Array (
    [name] => Array
        (
            [0] => foo.txt
            [1] => bar.txt
        )

    [type] => Array
        (
            [0] => text/plain
            [1] => text/plain
        )

    [tmp_name] => Array
        (
            [0] => /tmp/phpYzdqkD
            [1] => /tmp/phpeEwEWG
        )

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

    [size] => Array
        (
            [0] => 123
            [1] => 456
        )
)

It's not convenient! Agree? We need something more obvious like that:

Array(
    [0] => Array
        (
            [name] => foo.txt
            [type] => text/plain
            [tmp_name] => /tmp/phpYzdqkD
            [error] => 0
            [size] => 123
        )

    [1] => Array
        (
            [name] => bar.txt
            [type] => text/plain
            [tmp_name] => /tmp/phpeEwEWG
            [error] => 0
            [size] => 456
        )
)

To get arrays like that let's use this decorator-function:

function reorder_upload_files_array($file_post) {

    $file_array = array();
    $file_count = count($file_post['name']);
    $file_keys  = array_keys($file_post);

    for ($i = 0; $i < $file_count; $i++) {
        foreach ($file_keys as $key) {
            $file_array[$i][$key] = $file_post[$key][$i];
        }
    }

    return $file_array;
}

Usage:

$files = reorder_upload_files_array($_FILES)

hakre
  • 193,403
  • 52
  • 435
  • 836
Max
  • 1,824
  • 13
  • 22
  • Yes, I'm currently at that stage of debugging. I don't know what I was thinking, this seems so much like a beginners' question. – John Weisz Oct 25 '14 at 15:14
  • 1
    @János Weisz: It's not a problem to be a beginner. Key point is to ask and understand how it works. – hakre Oct 25 '14 at 15:20