简体   繁体   English

如何在php中一次移动上传的多个图像

[英]how to move uploaded multiple images at once in php

I'm trying to upload 3 images fields, store them in my database and move the uploaded file in a folder. 我正在尝试上传3个图像字段,将其存储在数据库中,然后将上传的文件移动到文件夹中。

For now, the "image1", 'image2", "image3", "image4" fields are inserting into MySQL database, but only "image1" is uploading and moving into my folder. 目前,“ image1”,“ image2”,“ image3”,“ image4”字段已插入MySQL数据库,但是只有“ image1”正在上载并移至我的文件夹中。

Here is my code: 这是我的代码:

if(isset($_POST['submit'])){
  $pro_image1 = $_FILES['image1']['name'];
  $pro_image2 = $_FILES['image2']['name'];
  $pro_image3 = $_FILES['image3']['name'];
  $pro_image4 = $_FILES['image4']['name'];
  $tmp_name   = $_FILES['image1']['tmp_name'];
  $tmp2_name  = $_FILES['image2']['tmp_name'];
  $tmp3_name  = $_FILES['image3']['tmp_name'];
  $tmp4_name  = $_FILES['image4']['tmp_name'];

  $pro_query = "INSERT INTO product(image1,image2,image3,image4) VALUES('$pro_image1','$pro_image2','$pro_image3','$pro_image4')";

  if(mysqli_query($con,$pro_query)){
    $msg = "<p class='pull-right' style='color:green;'> Product Added successfully</p>";
    $path = "images/$pro_image1";

    if(move_uploaded_file($tmp_name, $path)) {
      copy($path, "../$path");
    }

    if(move_uploaded_file($tmp2_name, $path)) {
      copy($path, "../$path");
    }

    if(move_uploaded_file($tmp3_name, $path)) {
      copy($path, "../$path");
    }

    if(move_uploaded_file($tmp4_name, $path)) {
      copy($path, "../$path");
    } elseif(!mysqli_query($con,$pro_query)) {
      $insert_error = "<p class='pull-right' style='color:red;>Product didn't added</p>";
    }
  }
}

<form action="" method="post" enctype="multipart/form-data"  class="form-font">
  <div class="col-md-6">
    <div class="form-group">
      <label>Image1</label>
      <input type="file" name="image1" class="form-control" required>
    </div>
    <div class="form-group">
      <label>Image2</label>
      <input type="file" name="image2" class="form-control" required>
    </div>
    <div class="form-group">
      <label>Image3</label>
      <input type="file" name="image3" class="form-control" required>
    </div>
    <div class="form-group">
      <label>Image4</label>
      <input type="file" name="image4" class="form-control" required>
    </div>
    <center>
      <input type='submit' name='submit' class='btn btn-success' value='Add Product'>
    </center>
  </div>
</form>

How to move the other images ("image2", "image3", "image4") in my folder? 如何移动文件夹中的其他图像(“ image2”,“ image3”,“ image4”)?
What is the mistake I made in my code? 我在代码中犯了什么错误? Could you please explain me where I am doing wrong ? 你能解释一下我做错了什么吗?

You have var $path defined only for image1 -- you need to change this var within each "if" block for functions move_uploaded_file and copy. 您仅为image1定义了var $ path -您需要在move_uploaded_file和copy函数的每个“ if”块中更改此var。 At the moment you use just path and filename for image1 for all images. 目前,您仅对所有图像使用image1的路径和文件名。

You just need to named them like "image[0], image[1], image[2]" Changes in your code 您只需将它们命名为“ image [0],image [1],image [2]”即可。

<form action="" method="post" enctype="multipart/form-data"  class="form-font">
    <div class="col-md-6">
       <div class="form-group">
           <label>Image1</label>
           <input type="file" name="image[0]" class="form-control" required>
       </div>
       <div class="form-group">
           <label>Image2</label>
           <input type="file" name="image[1]" class="form-control" required>
       </div>
       <div class="form-group">
            <label>Image3</label>
            <input type="file" name="image[2]" class="form-control" required>
       </div>
       <div class="form-group">
            <label>Image4</label>
            <input type="file" name="image[3]" class="form-control" required>
       </div>
       <center><input type='submit' name='submit' class='btn btn-success' value='Add Product'></center>
   </form>

please overrite your $path variable then it will upload property.. 请覆盖您的$ path变量,然后它将上传属性。

before if condition change $path varibale like below or change the variable to $path1,$path2,$path3.. 如果条件如下面那样更改$ path varibale或将变量更改为$ path1,$ path2,$ path3。

$path = "images/$pro_image1";
$path = "images/$pro_image2";
$path = "images/$pro_image3";

- --

        if(isset($_POST['submit'])){
   $pro_image1 = $_FILES['image1']['name'];
        $pro_image2 = $_FILES['image2']['name'];
        $pro_image3 = $_FILES['image3']['name'];
        $pro_image4 = $_FILES['image4']['name'];
        $tmp_name = $_FILES['image1']['tmp_name'];
        $tmp2_name = $_FILES['image2']['tmp_name'];
        $tmp3_name = $_FILES['image3']['tmp_name'];
        $tmp4_name = $_FILES['image4']['tmp_name'];
$pro_query = "INSERT INTO product(image1,image2,image3,image4) VALUES('$pro_image1','$pro_image2','$pro_image3','$pro_image4')";
        if(mysqli_query($con,$pro_query)){
            $msg = "<p class='pull-right' style='color:green;'> Product Added successfully</p>";
            $path = "images/$pro_image1";
            if(move_uploaded_file($tmp_name, $path)){
                 copy($path, "../$path");
               }
               $path = "images/$pro_image2";
               if(move_uploaded_file($tmp2_name, $path)){
                 copy($path, "../$path");
               }
               $path = "images/$pro_image3";
               if(move_uploaded_file($tmp3_name, $path)){
                 copy($path, "../$path");
               }
               $path = "images/$pro_image4";
               if(move_uploaded_file($tmp4_name, $path)){
                 copy($path, "../$path");
               }
               elseif(!mysqli_query($con,$pro_query)){
            $insert_error = "<p class='pull-right' style='color:red;>Product didn't added</p>";
        }
    }
}

<form action="" method="post" enctype="multipart/form-data"  class="form-font">
    <div class="col-md-6">
       <div class="form-group">
                <label>Image1</label>
                <input type="file" name="image1" class="form-control" required>
                </div>
                <div class="form-group">
                <label>Image2</label>
                <input type="file" name="image2" class="form-control" required>
                </div>
                <div class="form-group">
                <label>Image3</label>
                <input type="file" name="image3" class="form-control" required>
                </div>
                <div class="form-group">
                <label>Image4</label>
                <input type="file" name="image4" class="form-control" required>
                </div>
<center><input type='submit' name='submit' class='btn btn-success' value='Add Product'></center>
            </form>

Here is what you need to do: 这是您需要做的:

if(move_uploaded_file($tmp_name, $path)){
             copy($path, "images/$pro_image2");
           }
           if(move_uploaded_file($tmp2_name, $path)){
             copy($path, "images/$pro_image3");
           }if(move_uploaded_file($tmp3_name, $path)){
             copy($path, "images/$pro_image4");
           }if(move_uploaded_file($tmp4_name, $path)){
             //copy($path, "images/$path");
           }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM