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 } }?>
5
  • 3
    where is your upload script? which handles $_FILES['file_img']? Commented Jan 5, 2021 at 22:46
  • @Sysix Sorry missed that part, u can check it here: pastebin.com/raw/JnjU0y4J Commented Jan 5, 2021 at 22:51
  • Do i understand it correct? One file is uploaded correctly but multiple files are not? the pastebin code looks like only one file is processed. or is it not working at all?? so NO file is uploaded? Commented Jan 5, 2021 at 22:58
  • Only one file is uploading, I can't upload multiple, idk why. :/ Commented Jan 5, 2021 at 22:59
  • It is because there is no loop inside the pastebin upload code. Commented Jan 5, 2021 at 23:00

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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. :/
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.
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()) ....
The new pastebin code will not work as you use $_FILES instead of $file- please try to understand php.net/manual/de/control-structures.foreach.php and stackoverflow.com/questions/2704314/multiple-file-upload-in-php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.