note 119622 added to features.file-upload.multiple

From: Date: Tue, 19 Jul 2016 14:39:20 +0000
Subject: note 119622 added to features.file-upload.multiple
Groups: php.notes 
Request: Send a blank email to php-notes+get-206052@lists.php.net to get a copy of this message
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 } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 2804:7f2:80:70a3:c8e2:c11d:5549:9d38 ---- Manual Page -- http://php.net/manual/en/features.file-upload.multiple.php Edit -- https://master.php.net/note/edit/119622 Del: integrated -- https://master.php.net/note/delete/119622/integrated Del: useless -- https://master.php.net/note/delete/119622/useless Del: bad code -- https://master.php.net/note/delete/119622/bad+code Del: spam -- https://master.php.net/note/delete/119622/spam Del: non-english -- https://master.php.net/note/delete/119622/non-english Del: in docs -- https://master.php.net/note/delete/119622/in+docs Del: other reasons-- https://master.php.net/note/delete/119622 Reject -- https://master.php.net/note/reject/119622 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#206052) next »