Created
July 15, 2015 06:43
-
-
Save wangkeluo/449b663c4e6cc5e6b0c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| When uploading multiple files, the $_FILES variable is created in the form: | |
| Array | |
| ( | |
| [name] => Array | |
| ( | |
| [0] => foo.txt | |
| [1] => bar.txt | |
| ) | |
| [type] => Array | |
| ( | |
| [0] => text/plain | |
| [1] => text/plain | |
| ) | |
| [tmp_name] => Array | |
| ( | |
| [0] => /tmp/phpYzdqkD | |
| [1] => /tmp/phpeEwEWG | |
| ) | |
| [error] => Array | |
| ( | |
| [0] => 0 | |
| [1] => 0 | |
| ) | |
| [size] => Array | |
| ( | |
| [0] => 123 | |
| [1] => 456 | |
| ) | |
| ) | |
| I found it made for a little cleaner code if I had the uploaded files array in the form | |
| Array | |
| ( | |
| [0] => Array | |
| ( | |
| [name] => foo.txt | |
| [type] => text/plain | |
| [tmp_name] => /tmp/phpYzdqkD | |
| [error] => 0 | |
| [size] => 123 | |
| ) | |
| [1] => Array | |
| ( | |
| [name] => bar.txt | |
| [type] => text/plain | |
| [tmp_name] => /tmp/phpeEwEWG | |
| [error] => 0 | |
| [size] => 456 | |
| ) | |
| ) | |
| I wrote a quick function that would convert the $_FILES array to the cleaner (IMHO) array. | |
| <?php | |
| function reArrayFiles(&$file_post) { | |
| $file_ary = array(); | |
| $file_count = count($file_post['name']); | |
| $file_keys = array_keys($file_post); | |
| for ($i=0; $i<$file_count; $i++) { | |
| foreach ($file_keys as $key) { | |
| $file_ary[$i][$key] = $file_post[$key][$i]; | |
| } | |
| } | |
| return $file_ary; | |
| } | |
| ?> | |
| Now I can do the following: | |
| <?php | |
| if ($_FILES['upload']) { | |
| $file_ary = reArrayFiles($_FILES['ufile']); | |
| foreach ($file_ary as $file) { | |
| print 'File Name: ' . $file['name']; | |
| print 'File Type: ' . $file['type']; | |
| print 'File Size: ' . $file['size']; | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment