Avatar of Joey Drenth
Joey Drenth

asked on 

Upload multiple files from multiple inputs with PHP and insert the filename into a database.

I want to upload multiple files to my server with PHP and insert the filename, given description and some other things to my database.

This is the form I am using. I am using the LI and UL elements because I am using jQuery to add more inputs dynamically.

            <form class="add-post-form" action="add-files-to-post.php?id=<?php echo $lastId; ?>" method="post">
              <ul id="fieldList">
                  <input type='file' name='postFile'>
                  <input type='text' name='postName[]' placeholder='Titel van bestand'>
                  <input type='text' name='postDesc[]' placeholder='Beschrijving / instructie'>
              <button type="button" id="addMore" name="button">Nog een bestand</button>
              <input type="submit" name="" value="Bestanden uploaden en toevoegen">

Open in new window

This is the jQuery code

$(function() {
  $("#addMore").click(function(e) {
    $("#fieldList").append("<li><input type='file' name='postFile'></li>");
    $("#fieldList").append("<li><input type='text' name='postName[]' placeholder='Titel van bestand'></li>");
    $("#fieldList").append("<li><input type='text' name='postDesc[]' placeholder='Beschrijving / instructie'></li>");

Open in new window

This is how my table looks

Along with this, I am of course using some CSS to remove the bullet points and do some additional styling.

My question

How can I upload the files, while ALSO inserting the filepath, file title and file description into my database?

Using a loop seems the most logical option? However I am unsure on how to implement it.

I have tried a script that would upload multiple files from one <input> tag with the attribute multiple but this is not what I am looking for since I need to be able to add a title and description for each individual file.

Any help and tips are greatly appreciated. I know it's a broad question, but I just need someone to give me a point to start at.

Avatar of undefined
Last Comment
Joey Drenth

8/22/2022 - Mon