Once I had to do a maintenance in a huge ERP that had several multiple upload inputs inside an array. Just like this:
<form method="post" enctype="multipart/form-data">
<input type="file" multiple name="upload[avatar]" />
<input type="file" multiple name="upload[attachment]" />
<input type="file" multiple name="upload2[avatar]" />
<input type="file" multiple name="upload2[attachment]" />
<input type="submit" />
</form>
The $_FILES array is created like this:
Array
(
[upload] => Array
(
[name] => Array
(
[avatar] => teste.c
[attachment] => teste
)
[type] => Array
(
[avatar] => text/x-csrc
[attachment] => application/octet-stream
)
[tmp_name] => Array
(
[avatar] => /opt/lampp/temp/phpuf3KNj
[attachment] => /opt/lampp/temp/php0yPZap
)
[error] => Array
(
[avatar] => 0
[attachment] => 0
)
[size] => Array
(
[avatar] => 1960
[attachment] => 8661
)
)
[upload2] => Array
(
[name] => Array
(
[avatar] => jefrey.html
[attachment] => notas.txt
)
[type] => Array
(
[avatar] => text/html
[attachment] => text/plain
)
[tmp_name] => Array
(
[avatar] => /opt/lampp/temp/php87nfyu
[attachment] => /opt/lampp/temp/phpUBlvVz
)
[error] => Array
(
[avatar] => 0
[attachment] => 0
)
[size] => Array
(
[avatar] => 583
[attachment] => 191
)
)
)
I've managed to re-arrange this array like this:
Array
(
[upload] => Array
(
[avatar] => Array
(
[name] => teste.c
[type] => text/x-csrc
[tmp_name] => /opt/lampp/temp/phpuf3KNj
[error] => 0
[size] => 1960
)
[attachment] => Array
(
[name] => teste
[type] => application/octet-stream
[tmp_name] => /opt/lampp/temp/php0yPZap
[error] => 0
[size] => 8661
)
)
[upload2] => Array
(
[avatar] => Array
(
[name] => jefrey.html
[type] => text/html
[tmp_name] => /opt/lampp/temp/php87nfyu
[error] => 0
[size] => 583
)
[attachment] => Array
(
[name] => notas.txt
[type] => text/plain
[tmp_name] => /opt/lampp/temp/phpUBlvVz
[error] => 0
[size] => 191
)
)
)
Here's my snippet:
<?php
function reArrayFilesMultiple(&$files) {
$uploads = array();
foreach($_FILES as $key0=>$FILES) {
foreach($FILES as $key=>$value) {
foreach($value as $key2=>$value2) {
$uploads[$key0][$key2][$key] = $value2;
}
}
}
$files = $uploads;
return $uploads; // prevent misuse issue
}
?>