5
I am trying to upload files via ajax and PHP with the following codes below and it is not working. I can’t identify what I’m doing wrong.
Page
<div id="bsUpload">
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload[]" class="fileUpload"><br>
<input type="file" name="fileUpload[]" class="fileUpload"><br>
<input type="file" name="fileUpload[]" class="fileUpload"><br>
<br>
<input type="button" name="btEnviar" value="Fazer Upload">
</form>
Javascript
var $jq = jQuery.noConflict();
$jq(function(){
var bsUpload = $jq("#bsUpload");
bsUpload.on('click', 'input[name="btEnviar"]', function(event){
event.preventDefault();
formdata = new FormData(this);
$jq.ajax({
type: 'POST', cache: false, processData: false, contentType: false,
url: 'upload.php', data: formdata,
success: function(j){
alert(j);
}
});
});
});
PHP
<?php
foreach($_FILES['fileUpload']['error'] as $key => $error){
if($error == UPLOAD_ERR_OK){
$name = $_FILES['fileUpload']['name'][$key];
move_uploaded_file($_FILES['fileUpload']['tmp_name'][$key], '../../upload/'.$name);
}
}
echo 'Envio OK';
?>
The Error:
Notice: Undefined index: fileUpload in C: xampp htdocs examples upload-ajax Modeloc js Ajax upload.php on line 3
Warnig: Invalid argument supplied for foreach() in C: xampp htdocs examples upload-ajax Modeloc js Ajax upload.php on line 3
Worked
Thank you very much for your personal help
What exactly isn’t working? Gives some error?
– André Ribeiro