0

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

aim0d
  • 129
  • 7
  • Reloading a page that resulted from a POST request, simply sends that exact same request again. – CBroe May 03 '22 at 08:39
  • _"but the directory where it should do that it remain just one copy"_ - it looks like the file name you are trying to store it under is always the same, so that would be only natural - a directory can not contain two files with the exact same name ... – CBroe May 03 '22 at 08:41
  • And the countermeasure for this behaviour is called "GET after POST", eg. after receiving the POST and successfully processing it, redirect the user back to your page... a reload will then trigger the GET request, but not the POST – Honk der Hase May 03 '22 at 08:42
  • @CBroe I'm aware of that, but I dont know how to change it in order to upload my file just one and still using POST method – aim0d May 03 '22 at 08:44
  • @LarsStegelitz my page reload once I press the UPLOAD button, but it doesnt update the table so I try to reload and it's there, but then if for other stuff I reload it the same DOC appears – aim0d May 03 '22 at 08:46

0 Answers0