Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nicho's avatar
Level 1

How to upload multiple files using PHP postgresql

I'm trying to create a form where user can upload multiple file. When i tried to upload a file and add another file, the old file got replace by the new file that i just upload.

Html :

<form name="form1" method="post" action="form.php?mode=add" enctype="multipart/form-data" onSubmit="return check();">
<div class="form-group row">
    <label class="col-3 col-form-label">Dokumen Sketsa Kejadian</label>
    <input type="file" name="dokumen_sketsa_kejadian[]" id="dokumen_sketsa_kejadian" multiple>
  </div>
</form>

The store logic :

$jenis_dokumen_sketsa = 'Sketsa Kejadian';

    $dir = "../uploads/";
    if(isset($_FILES['dokumen_sketsa_kejadian'])){
      $totalFiles = count($_FILES['dokumen_sketsa_kejadian']['name']);

      for($i = 0; $i<$totalFiles; $i++){
        $dokumenSketsa = $_FILES['dokumen_sketsa_kejadian']['name'][$i];
        $file = $_FILES['dokumen_sketsa_kejadian']['tmp_name'][$i];

        move_uploaded_file($file, $dir.$dokumenSketsa);

        $queryInsertFile = "INSERT INTO dokumen_kecelakaan(id_kecelakaan, jenis_dok_kecelakaan, dokumen)
        VALUES (currval('kecelakaan_seq'), , )"; // Assuming you're using a sequence to generate id_kecelakaan

        $resultInsert = pg_query_params($connection, $queryInsertFile, array($jenis_dokumen_sketsa, $dokumenSketsa));

        if (!$resultInsert) {
            echo "Error in SQL query: " . pg_last_error();
            die();
        }
      }
    }
    echo "<META HTTP-EQUIV=\"refresh\" content=\"0; URL=list.php\">";

0 likes
2 replies
Snapey's avatar

learn Laravel . It will make your code significantly cleaner and easier to understand

Tray2's avatar

The code for the upload would look the same regardless of the database used, the only differencewould be the connection to the database.

Please or to participate in this conversation.