Search code examples
phpmysqlarraysmultifile-uploader

multiple image upload using foreach loop and inserting to database


I am having trouble getting the image files I wanted them to insert in my database I want the picture like the one I uploaded like this:

insert into('pic1.jpg');

insert into('pic2.jpg');

insert into('pic3.jpg');

not like this:

insert into(Array[0] => pic1.jpg, Array[1] => pic2.jpg, Array[2] => pic3.jpg);

so i can get their name only and insert in in my database im required to use foreach loop

  if(isset($_POST['upload'])){
      $upload[] = ($_FILES['images']['name']);
      print_r($upload);
   }


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form method="POST" enctype="multipart/form-data">        
        <input type="file" name="images[]" multiple="multiple">
        <input type="submit" name="upload" value="upload">
        </form>
    </body>
</html>

Solution

  • Try this ...

       foreach ($_FILES['image']['tmp_name'] as $key => $val ) {
    
            $filename = $_FILES['image']['name'][$key];
            $filesize = $_FILES['image']['size'][$key];
            $filetempname = $_FILES['image']['tmp_name'][$key];
    
            $fileext = pathinfo($fileName, PATHINFO_EXTENSION);
            $fileext = strtolower($fileext);
    
            // here your insert query
        }