I am trying to upload an image file to the server, but the $_FILE is always empty and I don't know why.
Here is the html:
<form action='' id='ajax_form' method='post' enctype="multipart/form-data">
<input type='file' name='imagem'>
<input type='submit' id='btUploadImagem' value='Upload'>
</form>
And here the jquery script:
function salvaImagem(){
var form = $(this).closest('form');
var formData = new FormData(form);
var dados = formData;
jQuery.ajax({
type: "POST",
url: "salvaImagem",
data: dados,
contentType: false,
processData: false,
dataType: "json"}).done(function(response)
{
if(response.sucesso){
//show success message
}
else{
//show error message
}
});
return false;
}
$('#btUploadImagem').on('click', salvaImagem);
And the controller php:
public function salvaImagem()
{
$arquivo = isset($_FILES["imagem"]) ? $_FILES["imagem"] : FALSE;
//some other checks
}
What am I doing wrong so the $_FILE is always empty?