I have a problem. I want to be able to upload one or more files from html and then with ajax and php to save them on my server. If i just want to upload one file, it works fine, but if I want to upload more than one it doesn't work. The thing is, i have a table with 4 options and the last one is upload. But there is an ADD button under that, so when I click on that button another table (the same table) apperas, and there is where I have the problem. This is the HTML code part:
<table id="myTable" class=" table order-list">
<tbody id="personalInfo">
<tr>
<td class="col-sm-2">
<input type="text" class="form-control" name="name0"
placeholder="<?php echo $translate['name'][$lang]?>">
</td>
<td class="col-sm-2">
<input type="text" class="form-control" name="surname0"
placeholder="<?php echo $translate['surname'][$lang]?>">
</td>
<td class="col-sm-2">
<input type="text" class="form-control datepickerYear" name="age0" autocomplete="off"
placeholder="2000" required />
</td>
<td class="col-sm-2">
<div class="pt-1">
<input type="file" class="form-control" id="UploadFile" name="UploadFile0" accept="application/pdf" required />
</div>
</td>
<td class="col-sm-1"><a class="deleteRow"></a>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" style="text-align: left;">
<input type="button" class="btn btn-danger " id="addRow"
value="<?php echo $translate['add'][$lang]?>" />
</td>
</tr>
<tr>
</tr>
</tfoot>
</table>
Okey and then I use JS to add another row when I click the ADD button. Everything works fine, but with the file I do this:
var fileCounter = 0;
const fileSelector2 = document.getElementById('UploadFile');
var file2;
fileSelector2.addEventListener('change', (event) => {
file2 = event.target.files[fileCounter];
fileCounter++;
});
And then with JS i use ajax to send it a POST to php. The php part is this:
$name=$json->fullName ;
$location = "../location/" .$name.$cont. ".pdf";
if (!move_uploaded_file($_FILES['file2']['tmp_name'], $location)) {
echo json_encode(-1);
exit();
}
Like I said, when I just upload one file, it works fine, but with more than one file I don't know how to save t and use POST with multiple files.