Question

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>
Was it helpful?

Solution 2

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
    }

OTHER TIPS

This is also one method:

foreach ($_FILES['images']['name'] as $f => $name) {
 $filename = $_FILES['images']['name'];
 $mysql_query1 = mysql_query("insert into table values('".$filename."')");
}
$images = $_FILES['images']['name'];
$sql = "insert into table (image_name) VALUES  ('".  implode("') ('", $a)."')";

Result is like :

insert into table (image_name) VALUES ('1.jpg') ('2.jpg') ('3.jpg') ('4.jpg')

user foreach.

example code :

foreach ($image as $image_path)
{
    mysql_query("insert into `table` (column) values ('".$image_path."')");
}

Try this...

if(isset($_POST['upload'])){
 $total = count($_FILES['images']['name']);
 for($i=0; $i<$total; $i++){

  $tmpFilePath=$_FILES['images']['tmp_name'][$i];
  if($tmpFilePath != ""){


    $img_name = $_FILES['images']['name'][$i];

    mysqli_query($connection, "INSERT INTO table_name (column_name) value ('".$img_name."')");
    }    
  }  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top