I would like to send multiple files via post using jQuery, I tried several codes and I could not, I would like you to help me.
I have this form:
HTML - index.html
<div class="arquivo" style="display: none;">
<form name="form-file" id="form-file" method="post" enctype="multipart/form-data">
<input type="file" multiple id="userfile" name="userfile[]" />
</form>
</div>
Javascript - send.js
$('#userfile').on('change', function (event) {
var data = new FormData(), file = [];
var files = event.target.files;
$.each(files, function(key, val) {
file[key] = val;
});
data.append('file', file);
$.ajax({
url: 'upload1.php',
type: 'post',
data: data,
contentType: false,
cache: false,
processData: false,
beforeSend: carregando,
success: function (result) {
// alert('Result: '+result);
$('#uploaded-files').html(result);
// sucesso('Arquivos enviados com sucesso!');
}
});
});
PHP
<?php
$path="arquivos/"; //server path
$result = '';
if(isset($_FILES['userfile']['name'][0])){
//echo $_FILES['userfile']['name'][0];
foreach ($_FILES['userfile']['name'] as $key => $file){
$file_name = 'arquivos/'.$_FILES['userfile']['name'][$key];
if(move_uploaded_file($_FILES['userfile']['tmp_name'][$key], $file_name)){
// $result .= "<div class='image'><img src='${file_name}'></div>";
$result .= "${file_name}<br />";
}
}
}else{
$result = "Nada";
}
?>
But in jQuery only returns "Nothing".
This same PHP code is used in Drag and Drop mode and works
Thanks in advance.