$ _FILES returning empty [duplicate]

0

This is the form I'm using to send the file

<form id="form_converter" enctype="multipart/form-data">
    <input type="file" name="arquivo" />
    <input type="submit" value="Enviar" />
</form>

$(document).on("submit", "#form_converter", function(){
    var dados  = $('#form_converter').serialize();
    jQuery.ajax({
        type: "POST",
        url: "sys/cv_unique_file.php",
        data: dados,
        success: function(data)
        {
            console.log(data);
        }
    }); 
    return false;
});

And in my PHP

if(isset($_FILES)){
    print_r($_FILES);
}

And I only get an empty array Array () , already check the permissions of the folders and everything is 777

    
asked by anonymous 12.04.2017 / 14:30

1 answer

0

The serialize() function does not include fields of type file in the generated query string, suppose that in your form you had 2 fields of type text , so serialize() would generate a string similar with this.

<form action="">
   <input type="text" name="nome">
   <input type="text" name="telefone">
   <input type="file" name="arquivo">
   <input type="submit" value="Enviar">
</form>

$('form').on('submit', function(event) {
   event.preventDefault();

   console.log($(this).serialize());
});

// Resultado do console.log: nome=aew&telefone=aew

This string generated by serialize() will be sent through ajax .

To send files using ajax you need to use the FormData class, like this:

$('form').on('submit', function(event) {
   event.preventDefault();

   var formData = new FormData(this);

   $.ajax({
     type: "POST",
     url: "sys/cv_unique_file.php",
     data: formData,
     success: function(data) {
        console.log(data);
     }
   }); 
});
    
12.04.2017 / 15:09