0

I am trying to give the user on my website the possibility to upload an infinite number of files by generating new <input type="file"> elements with Javascript (+ and - icon/button <i>):

<form action="action.php" name="form" method="post" enctype="multipart/form-data">
            <input id="file-upload-1" type="file" name="file1">
            <i id="upload-more" class="fas fa-plus"></i>
            <input id="file-upload-2" type="file" name="file2">
            <i id="upload-less" class="fas fa-minus"></i>
...
...
<input type='submit' name='submit-info' value='Submit Button'>
</form>

In my action.php file, I want to catch and save all the uploaded files to variables like this for 'file1':

if (isset($_POST['submit-info'])) {
   $file1 = $_FILES['file1'];
        $fileName1 = $_FILES['file1']['name'];
        $fileTmpName1 = $_FILES['file1']['tmp_name'];
        $fileSize1 = $_FILES['file1']['size'];
        $fileError1 = $_FILES['file1']['error'];
        $fileType1 = $_FILES['file1']['type'];
}

Since I don't know if the user is uploading 0, 5 or 100 files, how would I go about saving all user submitted files into variables in my action.php file? Help would be much appreciated.

sojutyp
  • 316
  • 1
  • 2
  • 11

1 Answers1

1

Instead of incrementing field names you can use a single field name as an array,

<input id="file-upload" type="file" name="file[]">

Then create as many instances as you want and read it by a for loop

Zortext
  • 566
  • 8
  • 21