0

I have this as HTML:

<input type="file" id="foo" multiple/>

This javascript:

$("#foo").change(function(){
   var files = $(this)[0].files; 
   $.ajax({
      // ???
   });
});

This PHP (upload.php):

if (!empty($_FILES)) {
    // Upload code

Currently the files are being read correctly from the input box. I want to use these files inside the upload.php. I have a different dropzone on my page which uses this upload.php. I don't want to write unneccesary double code so I would like to use this file again. However, I cannot send an ajax request with $_FILES.

TL;DR: How can I send an array with files with ajax to a PHP Url and use it there with $_FILES ?

Notes:
+ My input is not in a < form >
+ It is possible to select multiple files, so I need to pass multiple files to the php file

Thanks in advance!

1 Answer 1

1

You could try something like this:

var formData = new FormData();
for (var i = 0; i < files.length; i++) {
var file = files[i];
   formData.append('files[]', file, file.name);
}
$.post( 'somewhere', formData, callback);

Not the exact code, but it should get you started.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.