In work today, I noticed a distinct lack of solutions for uploading multiple files through the ‘media_handle_upload’ function. Since it assumes that $_FILES is just the normal array of details, it isn’t really adaptable to multi-file uploads.


So, to get around this, you can de-make and re-make several $_FILE arrays and just feed them through the function. Below is some example code for this:


if(isset($_POST['submit'])){ // Form has been upploaded 
 $count = count($_FILES['image_upload']['name']);
 for ($i=0; $i < $count; $i++) { 
 $newFiles[$i]["name"] = $_FILES['image_upload']['name'][$i];
 $newFiles[$i]["type"] = $_FILES['image_upload']['type'][$i];
 $newFiles[$i]["tmp_name"] = $_FILES['image_upload']['tmp_name'][$i];
 $newFiles[$i]["error"] = $_FILES['image_upload']['error'][$i];
 $newFiles[$i]["size"] = $_FILES['image_upload']['size'][$i];
for ($i=0; $i < $count; $i++) { 
 $_FILES = array();
 $_FILES['image_upload']['name'] = $newFiles[$i]["name"];
 $_FILES['image_upload']['type'] = $newFiles[$i]["type"];
 $_FILES['image_upload']['tmp_name'] = $newFiles[$i]["tmp_name"];
 $_FILES['image_upload']['error'] = $newFiles[$i]["error"];
 $_FILES['image_upload']['size'] = $newFiles[$i]["size"];
 $attachment_id = media_handle_upload( 'image_upload', 0 );
<div class="wrap">
 <form method="post" class="form-horizontal" enctype="multipart/form-data">
 <legend>Add Media</legend>
 <label for="image_upload" class="col-lg-3">(hold down ctrl and click each image/video for more than one)</label>
 <div class=" col-lg-5">
 <input type="file" name="image_upload[]" id="image_upload" class="form-control" multiple="multiple" />
 <input type="submit" class="btn btn-primary col-lg-3" name="submit" />

Leave a Reply