I am trying to upload files using web fetch api and PHP. But for some reason the files are not being captured (or sent?) from the web browser to the server. I can still send files with curl though.
async function send_files(url, files) { // files = [File(), File(), ...], url = localhost
const formData = new FormData()
for (const file of files) {
formData.append('files[]', file)
}
var result = await fetch(url, {
method: 'POST',
cors: 'same-origin',
headers: {
'Content-Type': 'multipart/form-data'
},
body: formData
})
return await result.json()
}
And in PHP if I print_r($_FILES) it returns an empty array. Now I know that uploading is working as curl confirms this:
curl -k -X POST -c ... -b ... -H "Content-Type: multipart/form-data" -F "files[]=@file1" -F "files[]=@file2" ... https://localhost
The request payload in the networking tab in my web shows:
------WebKitFormBoundaryXXXXXXXXXXXXXXXX
Content-Disposition: form-data; name="files[]"; filename="file1"
Content-Type: text/plain
------WebKitFormBoundaryXXXXXXXXXXXXXXXX
Content-Disposition: form-data; name="files[]"; filename="file2"
Content-Type: text/plain
------WebKitFormBoundaryXXXXXXXXXXXXXXXX--
The networking tab also shows I am only sending 372 B of info to the server, and the combined size of the two files are 941 B. I am not getting any errors either on front end or back end.
send_filesfunction, where does the content of thefilesparameter come from?send_filesis called in otherasync functions withawait send_json(...), thendata.json()as all my responses are json formatted. The files array is filled withFile()objects. This array is filled with an input type file that is captured with javascript and placed into an array. withfiles = [...files, ...event.target.files]. Along side that I have a drag and drop file that doesfiles = [...files, ...event.dataTransfer.files].application/x-www-form-urlencoded- if a form was submitted that way, only the file names of file input fields gets submitted.