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]);
}
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 - https://www.kvcodes.com/2013/12/create-front-end-multiple-file-upload-wordpress/
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.
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.
@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.