1

i want to upload multiple images url into database , after which i will move the file to an upload folder, then want to display the images and possibly edit by changing or removing them. i've been able to do that with one, but want to do that for multiple pics.

this is my 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>

Now my php script

  $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>';
    }

```

Kofi Amoussou
  • 82
  • 1
  • 1
  • 10

2 Answers2

2

Change input field to

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

Or

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

You will get array of selected images in $_FILES. You can loop through it and save it one by one.

Talha Malik
  • 1,509
  • 3
  • 22
  • 44
1

Here is what you need to do:

1.Input name must be be defined as an array i.e. name="inputName[]"

2.Input element must have multiple="multiple" or just multiple

3.In your PHP file use the syntax "$_FILES['inputName']['param'][index]"

4.Make sure to look for empty file names and paths, the array might contain empty strings

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

Community
  • 1
  • 1
JYoThI
  • 11,977
  • 1
  • 11
  • 26