Skip to content

Instantly share code, notes, and snippets.

@diversen
Last active June 16, 2021 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diversen/1cb3f6999998b5880158a119f14ba2a8 to your computer and use it in GitHub Desktop.
Save diversen/1cb3f6999998b5880158a119f14ba2a8 to your computer and use it in GitHub Desktop.
Snippet / function that rearrange php files when doing file uploads
<?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