使用foreach循环并插入数据库进行多个图像上传

时间:2014-01-29 06:07:45

标签: php mysql arrays multifile-uploader

我无法获取我希望它们插入我想要的数据库中的图像文件 像我上传的那样的照片:

insert into('pic1.jpg');

insert into('pic2.jpg');

insert into('pic3.jpg');

不喜欢这样:

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

所以我只能得到他们的名字并插入我的数据库 我需要使用 foreach循环

  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>

5 个答案:

答案 0 :(得分:3)

这也是一种方法:

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

答案 1 :(得分:2)

试试这个......

   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 :(得分:1)

$images = $_FILES['images']['name'];
$sql = "insert into table (image_name) VALUES  ('".  implode("') ('", $a)."')";

结果如下:

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

答案 3 :(得分:0)

用户foreach。

示例代码:

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

答案 4 :(得分:0)

试试这个......

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