如何将多个图像URL上传到数据库并将文件移动到文件夹

时间:2016-09-07 06:35:03

标签: php mysql arrays

我想将多个图片网址上传到数据库中,然后我将文件移动到上传文件夹,然后想要显示图片,并可能通过更改或删除它们进行编辑。我已经能够用一个,但想要为多个图片做到这一点。

这是我的HTML

<section class="form-group">
    <section class="col-md-12">

              <label for="price">Add Image</label>

                <input type="file" name="uploadedfile" id="uploadedfile" class="form-control"  />
                <input type="hidden" name="MAX_FILE_SIZE" value="100000">


    </section>
</section>

现在我的PHP脚本

  $target_path="uploads/";

 $target_path=$target_path.basename($_FILES['uploadedfile']['name']);
 if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

  }

     if($target_path==="uploads/"){
 $target_path="images/no-thumb.png";
       }



     $query="insert into properties
  (owner,email,contact,title,location,price,description,property_id,property_type,bedrooms,images,gsize,size,status,post_date) 
 values('$name','$email','$contact',
'$title','$location','$price','$description','$listid',$type','$bedroom','$target_path','$gsize','$size','$status',now())";


   $result=mysqli_query($dbc,$query);
   if($result){
    echo '<p class="bg-success alert alert-success text-center">Property Listing Successful</p>';
   }else{
   echo '<p class="bg-danger alert alert-danger">My bad!!! Something went totally wrong. Try again later while i try to fix it <span class="close pull-right"> <span class="close pull-right"> <a href="#" >&times; </a></span></p>';
    }

```

2 个答案:

答案 0 :(得分:1)

将输入字段更改为

<input type="file" multiple="true" name="uploadedfile[]" id="uploadedfile" class="form-control"  />

或者

<input type="file" multiple="multiple" name="uploadedfile[]" id="uploadedfile" class="form-control"  />

您将在$ _FILES中获得所选图像的数组。您可以循环浏览并逐个保存。

答案 1 :(得分:0)

以下是您需要做的事情:

1.输入名称必须定义为数组,即name =“inputName []”

2.Input元素必须有多个=“multiple”或只有多个

3.在PHP文件中使用语法“$ _FILES ['inputName'] ['param'] [index]”

4.确保查找空文件名和路径,该数组可能包含空字符串

HTML:

input name="upload[]" type="file" multiple="multiple" />

PHP:

        // Count # of uploaded files in array
    $total = count($_FILES['upload']['name']);

    // Loop through each file
    for($i=0; $i<$total; $i++) {
      //Get the temp file path
      $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

      //Make sure we have a filepath
      if ($tmpFilePath != ""){
        //Setup our new file path
        $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

        //Upload the file into the temp dir
        if(move_uploaded_file($tmpFilePath, $newFilePath)) {

          //Handle other code here

        }
      }
    }

source