In this code I send the formdata on an ajax request but the $_POST
array is empty on the destination file
('#uploadImgForm').submit(function(e){
e.preventDefault();
var files = $('#selectedFiles')[0].files;
var correct = true;
var form = $(this);
var data = new FormData();
//si contiene ficheros
if(files.length > 0){
Array.from(files).forEach(file => {
var filename = file.name.toLowerCase();
//recorrer los ficheros para comprobar que son imagenes
if(!(/^.+\.(gif|jpg|jpeg|png)$/i).test(filename))
correct = false;
else{
data.append(filename, file);
}
});
console.log(data);
//si todos son imagenes se envia el formulario
if(correct){
$.ajax({
url: 'imageUpload.php',
data: data,
cache: false,
contentType: false,
processData: false,
method: 'POST',
success: function(response){
console.log(response);
}
});
}else{//si hay algun fichero que no sea una iamgen ,avisar
alert("Los ficheros seleccionados deben ser imágenes");
}
}else{//si no contiene ficheros,avisar
alert("Debes seleccionar almenos una imagen");
}
});
I think its all correct, if I append another value to formdata (not a file) ,is received on teh destination file