note 113424 added to reserved.variables.files

From: Date: Wed, 09 Oct 2013 00:51:32 +0000
Subject: note 113424 added to reserved.variables.files
Groups: php.notes 
Request: Send a blank email to php-notes+get-196506@lists.php.net to get a copy of this message
I realize there are a number of posts here for reformating the php $_FILES array, but they don't handle all cases. This handles the single case, the multiple file case, and even submitting multiple file arrays. This way no matter what, before ever touching the files array I call this regardless of what it might be: <?php /** * This is to fix the odd files array PHP creates when a file input has a name that's php array: * eg: <file name="model[column]" value='file1'> <file name="model[column2][]" value='file2'> * becomes: $_FILES['model']['name'][column] = file1_name.xxx * $_FILES['model']['name'][column2][0] = file2_name.xxx * * this changes it to: * $files['model'][column]['name'] = file1_name.xxx * $files['model'][column2][0]['name'] = file2_name.xxx * * This way the file data is grouped together as expected and as it does with a non-array type name attribute */ static public function multi_file_fix($files = null) { if ($files == null) { $files = (is_array($_FILES)) ? $_FILES : array(); } //make there there is a file, and see if the first item is also an array $new_files = array(); foreach ($files as $name => $attributes) { if (is_array(reset($attributes))) { //check first item foreach ($attributes as $attribute => $item) { //array file submit, eg name="model[file]" foreach ($item as $key => $value) { if (is_array($value)) { foreach ($value as $key2 => $sub_val) { // multi-array file submit, eg name="model[file][]" $new_files[$name][$key][$key2][$attribute] = $sub_val; } } else { $new_files[$name][$key][$attribute] = $value; } } } } else { // regular file submit, eg name="file" $new_files[$name] = $attributes; } } return $new_files; } //Usage: $files = multi_file_fix($_FILES); ?> ---- Server IP: 69.147.83.199 Probable Submitter: 24.234.163.251 ---- Manual Page -- http://php.net/manual/en/reserved.variables.files.php Edit -- https://master.php.net/note/edit/113424 Del: integrated -- https://master.php.net/note/delete/113424/integrated Del: useless -- https://master.php.net/note/delete/113424/useless Del: bad code -- https://master.php.net/note/delete/113424/bad+code Del: spam -- https://master.php.net/note/delete/113424/spam Del: non-english -- https://master.php.net/note/delete/113424/non-english Del: in docs -- https://master.php.net/note/delete/113424/in+docs Del: other reasons-- https://master.php.net/note/delete/113424 Reject -- https://master.php.net/note/reject/113424 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#196506) next »