PHP to receive data from a CSV file via POST with Ajax

Asked

Viewed 66 times

1

I need to upload a CSV file via a form, pass to PHP using Ajax, parse the file and receive a return. I was able to send the file, but I can’t get the data from the file to analyze.

My CSV file:

"nome";E-mail;Data;100;[email protected];17/01/2021;

My code:

$("#enviar_file").click(function(){
    var formData = new FormData();
    var file = $('#file').val();
    formData.append('file', file);

    $.ajax({
        type: "POST",
        method: "POST",
        url: "update.php",
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        enctype: 'multipart/form-data',
        success: function(data) {
            $(".resultado").html(data);
        }
    });
    return false;
});
<div class="modal-body">
  <input type="file" id="file" name="file">
  <button type="submit" id="enviar_file">Upload</button>
</div>
<div class="resultado"></div>

My PHP code:

$tmpName = $_FILES['file'];
$handle = fopen($tmpName['tmp_name'], "r");
$row = 0;
while ($line = fgetcsv($handle, 1000, ";")){
    if ($row++ == 0){
        continue;
    }
            
    $people = [
        'email' => $line[2],
        'nome' => $line[1],
        'data' => $line[2]),
    ];
}
  • Why can’t you analyze the data? Error?

  • I can’t get the information with PHP, I did a test and returns empty, I couldn’t check if the CSV file is being passed correctly.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.