I am using this following code, where user can upload one or multiple files and can delete those files. All the data is stored in form_data.
Untill now I am not able to make the file upload functionality working.
index.php
<input id="avatar" type="file" name="avatar[]" multiple /> <button id="upload" value="Upload" type="button">upload</button> <div class="preview"> </div> <div class="return_php"></div> <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script> <script> $(document).ready(function () { var form_data = new FormData(); var number = 0; /* WHEN YOU UPLOAD ONE OR MULTIPLE FILES */ $(document).on('change', '#avatar', function () { console.log($("#avatar").prop("files").length); len_files = $("#avatar").prop("files").length; for (var i = 0; i < len_files; i++) { var file_data = $("#avatar").prop("files")[i]; form_data.append(file_data.name, file_data); number++; var construc = '<img width="200px" height="200px" src="' + window.URL.createObjectURL(file_data) + '" alt="' + file_data.name + '" />'; $('.preview').append(construc); } }); /* WHEN YOU CLICK ON THE IMG IN ORDER TO DELETE IT */ $(document).on('click', 'img', function () { var filename = $(this).attr('alt'); var newfilename = filename.replace(/\./gi, "_"); form_data.delete($(this).attr('alt')) $(this).remove() }); /* UPLOAD CLICK */ $(document).on("click", "#upload", function () { $.ajax({ url: "upload.php", dataType: 'script', cache: false, contentType: false, processData: false, data: form_data, // Setting the data attribute of ajax with form_data type: 'post', success: function (data) { $('.return_php').html(data); } }) }) }); </script> upload.php
<?php //upload.php var_export($_FILES); // this final output that i want to upload ?>