0

Here I am trying to insert multiple images into the database but they are not inserted. The images are uploading to the folder correctly but don't know why its not entering in to the database. Here is my image upload function

public function multiple_upload_files($path)
{
    $images = array();
    if(!empty($_FILES['files']['name'])){
        $filesCount = count($_FILES['files']['name']);
        for($i = 0; $i < $filesCount; $i++){
          $_FILES['file']['name'] = $_FILES['files']['name'][$i];
          $_FILES['file']['type'] = $_FILES['files']['type'][$i];
          $_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
          $_FILES['file']['error'] = $_FILES['files']['error'][$i];
          $_FILES['file']['size'] = $_FILES['files']['size'][$i];
          $config['upload_path']= './uploads/'.$path.'/';
          $config['allowed_types'] = 'gif|jpg|png|jpeg';
          $config['max_size']= '2000';
          $config['max_width'] = '4000';
          $config['max_height'] = '6500';
          $config['file_name']='upld-file'.time();
          $this->load->library('upload', $config);
          $this->upload->initialize($config);
         if ($this->upload->do_upload("file")) {

           $images[] = $this->upload->data();
        }
        else {  
         redirect('admin/view-product');

        }
    }
           return $images;
}

Here is my inserting code

$path='products';
     if($this->multiple_upload_files($path))
     {
      $img=implode(',',$images);
      $data = array('product_name' => $this->input->post('product_name'),'image'=>$img);
      $status = $this->Admin_model->db_insert($table='products',$data);
        if($status)
        {
          $this->session->set_flashdata('message','Product added Successfully');
        }
        else
        {
          $this->session->set_flashdata('message','Insertion failed');
        }

The problem is the images are uploaded to the uploaded folder but not to the database.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
user_777
  • 845
  • 1
  • 9
  • 25
  • 1
    Possible duplicate of [Multiple image upload with CodeIgniter](https://stackoverflow.com/questions/40778683/multiple-image-upload-with-codeigniter) – Mayank Vadiya Aug 26 '17 at 05:53

1 Answers1

2

You are returning array of images from method but not receiving it. change you code as below

 $path='products';
 $images= $this->multiple_upload_files($path);
 if($images)
 {
  $img=implode(',',$images);
  $data = array('product_name' => $this->input->post('product_name'),'image'=>$img);
  $status = $this->Admin_model->db_insert($table='products',$data);
    if($status)
    {
      $this->session->set_flashdata('message','Product added Successfully');
    }
    else
    {
      $this->session->set_flashdata('message','Insertion failed');
    }
B. Desai
  • 16,414
  • 5
  • 26
  • 47