Media handle upload multi-file upload form data fetch

Hello, How do you upload a file from formdata multi-file upload. I am sending data from JS formdata to PHP and then trying to upload using media_handle_upload. I can do this with file upload but not file uploads.

JS and PHP below

var ins = document.getElementById('lf_Input_3').files.length;
        for (var x = 0; x < ins; x++) {
            data.append("multifiles[]", document.getElementById('lf_Input_3').files[x]);
       $count = count($_POST['multifiles']);
             for ($i = 0; $i < $count; $i++) {
                 $file = $_POST['multifiles'][$i];
                 $lfattachmentid = media_handle_upload( $file, $post_id );

If you are asking about a javascript FormData object, the submitted data will be in $_FILES, and if you just reference the form when you create the FormData object, it will already have all the form field data in it. No need to ‘manually’ append individual things to it.

AFAIK, none of the WordPress file upload handling functions can directly deal with an array of uploaded files, because they internally, improperly, reference the $_FILES[‘field_name’] elements and assume a single file upload. The solution/kluge is to build your own $_FILES array of data for each uploaded file, then call the WordPress file upload handling function. See this link -

1 Like

You are on the right path and that you have to run an iteration against an array. The problem lies in both the HTML and the PHP files. If you don’t have the right HTML tag to indicate to the server that you’re going to upload more than 1 file, that’s a problem. For the PHP side, you have to run your loop against the $_FILES variable. You’ll also want to run it in the iteration the files are attached to. I’ll give you a hint, it looks something like $_FILES[$i][…].

I’ve actually tried that syntax using the index of the files and how they were sequentially uploaded to the form data. My form has all the correct attributes.

The problem I think is the media handle upload only uploads a single file and it uses the name of the multi file upload input to upload the files. The problem is that the same name attribute can only be used one time. And I’m just not sure how to get around that.

What does this represent […] ?

Sending the form data VIA fetch. It handles the form data with AJAX/fetch.

Unfortunately, that’s not the array structure for multiple files. It’s $_FILES[…][$i]

The link I included shows how to build your own $_FILES array for each of the multiple uploaded files before calling the WP function(s.)

Represents the different elements, e.g. name, size, tmp_name, …

If you are POSTing the data, the type=‘file’ field data will be in $_FILES, unless you are doing something odd that you haven’t shown us in the snippets of code.

There’s a problem with the tutorial you shared. the fetch URL uses admin-ajax.php which all of WordPress Ajax forms use to post data. In the tutorial, it required a custom post.php file to grab the files data. The admin-ajax.php is not a file I believe I can modify.

This is something I’m trying to allure to to see if you can catch on. Don’t know why one would use JS to do this handling because HTML can do this perfectly fine. You’d have to set the name for your input field to actually use that syntax. So something like

<input type="file" name="multifiles[]" ...>

Then from your PHP file, you’d do something like what I said before.

$files = $_FILES['multifiles'];
$count = count($files);

for($i = 0; $i < $count; $i++) {

[...] means “etc” and can literally mean anything. It’s a placeholder for anything you want in there that actually applies to said array.

Correctly, sorry I was on lunch break and on my phone so I didn’t have time to check if it was one way or another.

@mabismad The snippet helped out a lot. I didn’t quite need everything in the snippet so had to modify it slightly with my configuration but it’s working now. Thanks for your insights.

@spaceshiptrooper I do believe the way you suggested would have worked if the site environment did not need to use media handle upload. I actually would have preferred to solve the problem with this solution but as @mabismad suggested the media handle upload assumes a single file upload field only.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.