multiple image upload using foreach loop and inser

2019-09-02 03:57发布

问题:

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>

回答1:

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
    }


回答2:

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."')");
}


回答3:

$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')


回答4:

user foreach.

example code :

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


回答5:

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."')");
    }    
  }  
}