在我的sql数据库中上传多个图片

时间:2018-03-12 12:49:33

标签: php mysql

我试图在PHP MySQL数据库中抛出多个图像上传。我的数据库名称是属性中的不动产和表格,我在属性图像中的一个字段我正在单个元素中的数据库中的多个图像存储中尝试。

这是我的代码:

<?php
    if(isset($_POST['Submit'])){
        extract($_POST);
        $property_image=$_FILES['property_image']['name'];


     $sql="insert into property set property_title='".$property_title."',property_price='".$property_price."',property_image='".$property_image."',bedrooms='".$bedrooms."',bathrooms='".$bathrooms."',property_decription='".$property_decription."',property_address='".$property_address."',rooms='$rooms'"; 

            $query=query($sql)or die(mysqli_error($con)

             if(count($_FILES["property_image"])>0){
                }else{
               } 
      $valid_image_check = array("image/gif", "image/jpeg", "image/jpg", "image/png", "image/bmp");
      for($i =0;$i<count($_FILES["property_image"]["name"]);$i++){
        $image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_FILES["property_image"]["tmp_name"][$i])));
        if (in_array($image_mime, $valid_image_check)) {

  } else {

  }

}
      $foldarName="uploads/";
      $ext = explode("/", strtolower($image_mime));
      $ext = strtolower(end($ext));
      $filename = rand(10000, 990000) . '_' . time() . '.' . $ext;

       $filepath = $uploads . $property_image;

if (!move_uploaded_file($_FILES["property_image"]["tmp_name"][$i], $filepath)) {

} else {
  }
      }
                ?>

这是我的HTML:

<label>Property Images*</label>  
 <input type="file" name="property_image" id="property_image" multiple="">

1 个答案:

答案 0 :(得分:0)

您甚至在循环文件之前插入数据

<?php
$connection = null; // your database connection
$files = $_FILES;

foreach ($files as $file) {
    $data = file_get_contents($_FILES['name_of_control']['tmp_name']);
    $safeData = mysqli_real_escape_string($connection, $data);
    $sql = "INSERT INTO images ('image') VALUE ($safeData)";
    // execute this query
}
?>
<!doctype html>
<html lang="en">
<head>
</head>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
    <input type="file" name="files" multiple>
    <button type="submit">Submit</button>
</form>
</body>
</html>

因此,您需要在循环浏览文件后存储,您可以将所有值收集到sql查询中并执行。