I'm trying to upload files via ajax and PHP with the following codes below and it's not working. I can not 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 \ TemplateC \ js \ Ajax \ upload.php on line 3
Warnig: Invalid argument supplied for foreach () in C: \ xampp \ htdocs \ examples \ upload-ajax \ TemplateC \ js \ Ajax \ upload.php on line 3
It worked out
Thank you for the personal help