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.

1

1 Answer 1

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');
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.