2

This code uploads multiple images with html and php.

This is my HTML code:

 <form action="<?php echo site_url('account/add_gallery1');?>" method="post" enctype="multipart/form-data" >
     <input type="hidden" name="user_id" value="<?php echo $profile->id;?>" id="user_id"/>
     <input type="file" name="userfile[]" multiple>
     <button type="submit" class="btn btn-default save_change_btn">upload photo</button>
 </form>

This is my PHP code:

if($_FILES['userfile']['name'] != "") {
  if(isset($_FILES['userfile'])) {
     $j = 0;     // Variable for indexing uploaded image.

                $target_path = DOCUMENT_ROOT."uploads/test/";     // Declaring Path for uploaded images.

                for ($i = 0; $i < count($_FILES['userfile']['name']); $i++) {

                    // Loop to get individual element from the array

                    $validextensions = array("jpeg", "jpg", "png");      // Extensions which are allowed.

                    $ext = explode('.', basename($_FILES['userfile']['name'][$i]));   // Explode file name from dot(.)

                    $file_extension = end($ext); // Store extensions in the variable.

                    $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];     // Set the target path with a new name of image.

                    $j = $j + 1;      // Increment the number of uploaded images according to the files in array.


                    if (($_FILES["userfile"]["size"][$i] < 2000000)
                        && in_array($file_extension, $validextensions)) {

                        if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $target_path)) {

                            // If file moved to uploads folder.

                            $cpt = count($_FILES['userfile']['name'][$i]);
                            for($i=0; $i<$cpt; $i++) {
                                $images = $_FILES['userfile']['name'];

                            }
                            $names= implode(',', $images);
                            $data = array(
                                'user_id' => $user_id,
                                'image_name' => $names
                            );
                            //print_r($data);die;
                            $this->user_model->insert_gallery_images($data);
                        } else {
                            echo $j. ')'."File Not Moved.";

                        }
                    }  else {
                        echo $j. ')'."Invalid file Size or Type";

                    }


                }
  }
}

in model:

function insert_gallery_images($var)
{
    $this->db->insert('dbc_gallery_images',$var);
}

problem: My problem is how can I get images name from $_FILES['userfile']['name'][$i] and store one image name in a database? for example i have one table galeery_images which have three fields id,user_id and image_name. from this problem i get image name but which is in this format

 id      user_id    image_name
-----   --------   ------------
  1        12    coach_1.jpg,coach_2.jpg,coach_3.jpg,coach_4.jpg

but i want image store like this.

 id      user_id    image_name
-----   --------   ------------
  1        12    coach_1.jpg
  2        12    coach_2.jpg  
  3        12    coach_3.jpg 
  4        12    coach_4.jpg 
K.doe
  • 79
  • 2
  • 11
  • 1
    It's a bad pratice to store images directly on db, you should for example store the files on the hdd and save on the db only the path. – Claudio King Sep 16 '16 at 09:28
  • okay.. i accept your suggestion can you explain me how can i store images now? – K.doe Sep 16 '16 at 09:41
  • you can store images in mysql as a blob i believe but as @Claudio King said best to save the images in a folder and store the path in the database. So Please clarify are you asking now how to upload an image and store the path in a database – kerry Sep 16 '16 at 09:52
  • yes, i want to store image path in a database – K.doe Sep 16 '16 at 09:56
  • please give me solution as soon as possible. – K.doe Sep 16 '16 at 10:02
  • There are 2 problems with code (at least): 1. remove die; 2. add a dash (-) after user_model: $this->user_model >insert_gallery_images($user_id,$_FILES['userfile']['name'][$i]); – Rouhollah Mazarei Sep 17 '16 at 13:27
  • no still i have problem and i edit my problem in my question – K.doe Sep 19 '16 at 05:10

0 Answers0