Processing the multidimensional array $ _FILES

I have the following $_FILES array passed for processing from the support form

 Array ( [file] => Array ( [name] => Array ( [0] => Test.jpg [1] => Test.doc [2] => Test.php [3] => ) [type] => Array ( [0] => image/jpeg [1] => image/jpeg [2] => image/jpeg [3] => ) [tmp_name] => Array ( [0] => /tmp/phpCO0vSD [1] => /tmp/phpEFpp3Q [2] => /tmp/phpwN4Iwc [3] => ) [error] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 4 ) [size] => Array ( [0] => 1305787 [1] => 339773 [2] => 480098 [3] => 0 ) ) ) 

My main problem is to understand the logic needed to process the array, check each file (which I already have a list of valid extensions), and then rename and save the file accordingly.

Google and SO solutions are pretty complicated for my simple requirements.

+4
source share
4 answers

Here's how you could move around your array:

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

With this loop, you can easily do what you want.

+6
source

I adore foreach() so much, I always go with this ...

 $files = array(); foreach ($_FILES['files']['name'] as $num_key => $dummy) { foreach ($_FILES['files'] as $txt_key => $dummy) { $files[$num_key][$txt_key] = $_FILES['files'][$txt_key][$num_key]; } } 

... and then again using foreach($files as $file) to work well with the data. :)

Of course, the answer from zerkms is faster, but it is a general approach to reorder the logic of the array for any case - you need to go through it twice (x * y).

+5
source

Here is a more general solution to seoguru's answer . It works with file input fields, the name of which contains any level of nested arrays, for example file , file[] (case for this question), file[english] , file[english][] , etc.

 function rearrangeUploadArray(array $array) { if(!is_array(reset($array))) return $array; $rearranged = []; foreach($array as $property => $values) foreach($values as $key => $value) $rearranged[$key][$property] = $value; foreach($rearranged as &$value) $value = rearrangeUploadArray($value); return $rearranged; } 

I understand that this answer is more complicated than it should be for this question, but maybe it can someday be useful to someone. An example of use is a localized upload form where several files can be downloaded for different languages. Then it would be advisable to have one file input field for each language, named, for example, file[english][] , file[german][] , file[spanish][] , etc. rearrangeUploadArray($_FILES['file']) will then return an array of the form

 Array ( [english] => Array ( [0] => Array ( [name] => ... [type] => ... [tmp_name] => ... [error] => ... [size] => ... ) [1] => Array ( [name] => ... [type] => ... [tmp_name] => ... [error] => ... [size] => ... ) [...] ) [german] => Array ( [0] => Array ( [name] => ... [type] => ... [tmp_name] => ... [error] => ... [size] => ... ) [...] ) [spanish] => Array ( [0] => Array ( [name] => ... [type] => ... [tmp_name] => ... [error] => ... [size] => ... ) [...] ) ) 
+2
source

Another recursive reordering option based on clamcrusher solution from here

 function getFixedFilesArray() { $walker = function ($arr, $fileInfokey, callable $walker) { $ret = array(); foreach ($arr as $k => $v) { if (is_array($v)) { $ret[$k] = $walker($v, $fileInfokey, $walker); } else { $ret[$k][$fileInfokey] = $v; } } return $ret; }; $files = array(); foreach ($_FILES as $name => $values) { // init for array_merge if (!isset($files[$name])) { $files[$name] = array(); } if (!is_array($values['error'])) { // normal syntax $files[$name] = $values; } else { // html array feature foreach ($values as $fileInfoKey => $subArray) { $files[$name] = array_replace_recursive($files[$name], $walker($subArray, $fileInfoKey, $walker)); } } } return $files; } 

The main difference from the original solution is that I replaced array_merge_recursive with array_replace_recursive so that indexed arrays in the tree do not produce an invalid result (array_merge_recursive for indexed arrays adds new indexes instead of merging with existing indexes). I tested this in several rather difficult situations and worked as expected

+1
source

All Articles