I am trying to transfer the file in the following way:

$('input[type="file"]').on('change', function(e) { var nm = this.value.replace(/\\/g, '/').replace(/.*\//, '') var datf = this.files[0]; $.ajax({url: "sent.php", data: {'name': nm, 'file': datf}, processData: false, contentType: false, type: 'POST', success: function(ev) { alarm(ev); }})}) 

In the script sent.php, if you print the entire array $GLOBALS , then $_FILES and $_POST are empty. What is wrong here?
PS Something is in HTTP_RAW_POST_DATA - string(15) "[object Object]" What could be there?

    1 answer 1

    As I understand it, you should use FormData and its methods to transfer files through AYAX, for example, append - which adds a new value to an existing key inside a FormData object, or creates a key if it is missing.

    The append method has several parameters that it takes.

    • name - The name of the field that will contain the data from value.
    • value - the value
    • filename Необязательный - sets the name of the file that will be sent to the server.

    Those. if you add a file like this: formData.append(nm , this.files[0]); , then the file name will match the one that was filled, and if so: formData.append(nm , this.files[0], yoMySuperFileName); , then the file will have [name] => yoMySuperFileName

    Sending you will look like this:

     $('input[type="file"]').on('change', function(e) { var formData = new FormData(); var nm = this.value.replace(/\\/g, '/').replace(/.*\//, '') formData.append(nm , this.files[0]); $.ajax({ url: "get.php", data: formData, processData: false, contentType: false, type: 'POST', success: function(ev) { alarm(ev); } }) })