1

Someone can help me please with this code? I would add multiple images but I can't figure it out. I tried the multiple command but it doesn't worked, I have no clue honestly.

HTML code:

<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data" data-abide>
<div class="photo-field">
<input type="file" name="file_img" pattern="^.+?\.(jpg|JPG|png|PNG|jpeg|JPEG)$" required>
<small class="error">Upload JPG or PNG only.</small>
</div>
<div class="title-field">
<input type="text" name="img_title" placeholder="Image title" required>
<small class="error">Image title is required.</small>
</div>
<input type="submit" value="Upload Image" name="btn_upload" class="button">
</form>

PHP code:

 <li><a href="upload-photo.php" data-reveal-id="uploadModal" data-reveal-ajax="true">Add Photo</a></li>
           <li class="divider"></li>
           
        </ul>
     </section>
  </nav>
  <br/>
  <!--Content goes here-->
  <div class="row">
     <div class="large-12 columns">
        <?php
        if(isset($_GET['success'])) {
        if($_GET['success']=="yes"){?>
        <div class="row">
           <div class="small-6 large-6 columns">
              <div data-alert class="alert-box success radius ">
                 Image "<?= $_GET['title']; ?>" uploaded successfully.
                 <a href="#" class="close">&times;</a>
              </div>
           </div>
        </div>
        <?php } else {?>
         <div class="row">
           <div class="small-6 large-6 columns">
              <div data-alert class="alert-box alert radius ">
                 There was a problem uploading the image.
                 <a href="#" class="close">&times;</a>
              </div>
           </div>
        </div>
        <?php } }?>
Shadow
  • 33,525
  • 10
  • 51
  • 64
kedder13
  • 23
  • 4

1 Answers1

2

The problem is, that there is no loop in the processing php script.

So only one file gets uploaded.

For more info and solutions see: Multiple file upload in php

  • `foreach($_FILES as $file { { $filetmp = $_FILES["file_img"]$file["tmp_name"]; $filename = $_FILES["file_img"]["name"]; $filetype = $_FILES["file_img"]["type"]; $filepath = "img/" . $filename; $filetitle = $_POST['img_title']; move_uploaded_file($file[$filetmp], $file[$filepath]); } ` I tried this but unfortunately not working. :/ – kedder13 Jan 05 '21 at 23:36
  • PLEASE be more specific. "NOT WORKING" is no valuable info anyone can work with. Please also include the new code in your original post as it is very difficult to read here. Try to echo some important info during tvarious steps during he processing of the file to see where the problem is occuring. – Alexander Dobernig Jan 05 '21 at 23:42
  • You will also have to include the db updates into the loop BUT NOT THE PREPARE whicj should be done before. $stmt->bindValue(':iname', $filename); $stmt->bindValue('itype', $filetype); $stmt->bindValue('ipath', $filepath); $stmt->bindValue('ititle', $filetitle); if ($stmt->execute()) .... – Alexander Dobernig Jan 05 '21 at 23:45
  • The new pastebin code will not work as you use $_FILES instead of $file- please try to understand https://www.php.net/manual/de/control-structures.foreach.php and https://stackoverflow.com/questions/2704314/multiple-file-upload-in-php – Alexander Dobernig Jan 05 '21 at 23:49