I'm trying to upload some PDF on my Database and simultaneously on a table in my HTML.
The script is working just fine, but everytime I reload the page it upload the last file I choose, multiple time and not just once.
This is my HTML code for upload PDF:
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="myfile">
<button type="submit" name="save">upload</button>
</form>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Titolo</th>
<th>Autore</th>
<th>Data Pubblicazione</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($files as $file): ?>
<tr>
<td> <?php echo $file['id']; ?> </td>
<td> <a href="<?php echo $destination, $file['name'];?>"> <?php echo $file['name']; ?> </a> </td>
<td> <?php echo $file['autore']; ?> </td>
<td> <?php echo $file['data']; ?> </td>
</tr>
<?php endforeach;?>
</tbody>
</table>
And this is the one I use to Upload on my database:
<?php
require_once "config.php";
$sql = "SELECT * FROM pdf";
$result = mysqli_query($conn, $sql);
$files = mysqli_fetch_all($result, MYSQLI_ASSOC);
// Uploads files
if (isset($_POST['save'])) { // if save button on the form is clicked
// name of the uploaded file
$filename = $_FILES['myfile']['name'];
// destination of the file on the server
$destination = 'pdf/uploads/' . $filename;
// get the file extension
$extension = pathinfo($filename, PATHINFO_EXTENSION);
// the physical file on a temporary uploads directory on the server
$file = $_FILES['myfile']['tmp_name'];
//$data = $_FILES['myfile']['data'];
$author = $_SESSION['nome'];
$data = date("m/d/Y H:i");
/*if (!in_array($extension, ['zip', 'pdf', 'docx'])) {
} else {*/
// move the uploaded (temporary) file to the specified destination
if (move_uploaded_file($file, $destination)) {
$sql = "INSERT INTO pdf (id, name, autore, data) VALUES (null, '$filename', '$author','$data')";
if (mysqli_query($conn, $sql)) {
echo "<script>console.log(File uploaded successfully);</script>";
}
} else {
echo "Failed to upload file.";
}
}
?>
Can someone help me understand why it upload multiple times? I'm new to this world and at the moment I'm struggling.
NOTE I notice that when it upload the file the second/third/.. time it does it add to the database as well, but the directory where it should do that it remain just one copy
I saw the question you close my question for 2h ago and didnt help me, so pls reopen my question. Because I'm bagging my head since 3h trying to understand why it acts like that