note 103982 modified in features.file-upload.multiple by danbrown

From: Date: Mon, 16 May 2011 16:16:38 +0000
Subject: note 103982 modified in features.file-upload.multiple by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-179768@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 */ $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; }; ?> --was-- 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; }; ?> http://php.net/manual/en/features.file-upload.multiple.php

« previous php.notes (#179768) next »