note 103982 added to features.file-upload.multiple

From: Date: Sun, 15 May 2011 19:23:33 +0000
Subject: note 103982 added to features.file-upload.multiple
Groups: php.notes 
Request: Send a blank email to php-notes+get-179739@lists.php.net to get a copy of this message
This a solution, using php closures (version >= 5.3), to convert the weird indexes schema of $_FILES. Very useful in case you have multiple indexes in the name of the html input element (e.g name="form1[set1][file1][]"), or if you just want to handle multiple files per element. <?php /** * Fix the $_FILES order by converting from wierd schema * [firstel]["name"][subel][0], [firstel]["size"][subel][0] ... [firstel]["error"][subel][0] * * to a more straightforward one. * [firstel][subel][0]["name"], [firstel][subel][0]["size"] ... [firstel][subel][0]["error"] * * @author Sque * @link http://code.0x0lab.org/p/cm5 */ $fix_files_keys = function($files) use (& $fix_files_keys) { if (isset($files['name'], $files['tmp_name'], $files['size'], $files['type'], $files['error'])){ // Multiple values for post-keys indexes $move_indexes_right = function($files) use(& $move_indexes_right) { $results = array(); foreach($files['name'] as $index => $name) { $reordered = array( 'name' => $files['name'][$index], 'tmp_name' => $files['tmp_name'][$index], 'size' => $files['size'][$index], 'type' => $files['type'][$index], 'error' => $files['error'][$index], ); // If this is not leaf do it recursivly if (is_array($name)) $reordered = $move_indexes_right($reordered); $results[$index] = $reordered; } return $results; }; return $move_indexes_right($files); } // Re order pre-keys indexes array_walk($files, function(&$sub) use(& $fix_files_keys) { $sub = $fix_files_keys($sub); }); return $files; }; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 212.54.199.43 ---- Manual Page -- http://www.php.net/manual/en/features.file-upload.multiple.php Edit -- https://master.php.net/note/edit/103982 Del: integrated -- https://master.php.net/note/delete/103982/integrated Del: useless -- https://master.php.net/note/delete/103982/useless Del: bad code -- https://master.php.net/note/delete/103982/bad+code Del: spam -- https://master.php.net/note/delete/103982/spam Del: non-english -- https://master.php.net/note/delete/103982/non-english Del: in docs -- https://master.php.net/note/delete/103982/in+docs Del: other reasons-- https://master.php.net/note/delete/103982 Reject -- https://master.php.net/note/reject/103982 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179739) next »