Last active
June 16, 2021 08:40
-
-
Save diversen/1cb3f6999998b5880158a119f14ba2a8 to your computer and use it in GitHub Desktop.
Snippet / function that rearrange php files when doing file uploads
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
<?php | |
/** | |
* Takes the input file element name, e.g. `$files = rearrange_files($_FILES['files']);` | |
* If you have a file input like this `<input type="file" name="files[]" multiple >` | |
* Will return an array where each element is a single file. Much more convenient thant | |
* Using the PHP $_FILES array | |
*/ | |
function rearrange_files($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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment