In my project I take the file from an html file input (in a modal), then I pass it with ajax to php and with the $_FILE super variable I get his content:
The modal
<form id="form_admin_panel_import" enctype="multipart/form-data" method="post">
<input type="file" class="form-control" name="usersFile" id="usersFile">
<button type="submit" class="btn btn-primary" id="buttonAction">Import
</button>
</form>
Js and Ajax
$( "#form_admin_panel_import" ).submit(function( event ) {
event.preventDefault();
var fd = new FormData(this);
$.ajax({
url: (URL + "managementUsers/importUsers"),
type: "POST",
data: fd ,
processData: false,
contentType: false,
success: function (result) {
console.log(result);
}
});
});
php
if ( 0 < $_FILES['usersFile']['error'] ) {
echo 'Error: ' . $_FILES['usersFile']['error'] . '<br>';
}
else {
if (($handle = fopen($_FILES['usersFile']['tmp_name'], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$firstName = $data[0];
}
}
The problem is that when I have special chars in the csv file the php receives a ? char and I cannot get back to the original char. I think that I have to use an UTF8 Header but I don't know where...