2
votes

I want to upload two or more pictures using one form in separate fields into database using Codeigniter.

but here only one is uploading .. can anyone please help me with it..

My controller


class Products extends CI_Controller {

       public function __construct()
        {
                // Call the CI_Model constructor
                parent::__construct();

                $this->load->model('Product_model');


        }

    public function save()
            {
                    $config['upload_path']          = './uploads/';
                    $config['allowed_types']        = 'jpg|png';
                    $config['max_size']             = 5024;
                    $config['encrypt_name']         =TRUE;  


                    $this->load->library('upload', $config);

                    if ( ! $this->upload->do_upload('userfile'))
                    {
                            $error = array('error' => $this->upload->display_errors());

                            echo var_dump($error ); die;
                    }
                    else
                    {

                        $file_data = array('upload_data' => $this->upload->data());

                    if($this->Product_model->addProducts($file_data))
                    {


                        $this->load->view('success_view');
                    }
                    else
                    {


                        $this->load->view('failure_view');
                    }
                }

Here is My model


    public function addProducts($file_data)
            {
                    $data=array(
                            'pr_name'=>$_POST['pr_name'],

                            'pr_code'=>$_POST['pr_code'],



                            'photo_file'=>$file_data['upload_data']['file_name'],
                            'photo_file2'=>$file_data['upload_data']['file_name'],


                            );

                    return $this->db->insert('products', $data);
            }

Here is my View

    <div class="container">

<div class="row">
    <div class="col-md-6">
        <form class="form-horizontal" method="post" enctype="multipart/form-data" action="<?php echo site_url('Products/save');?>">
          <div class="form-group">
            <label for="exampleInputEmail1">Product Name</label>
            <input type="text" name="pr_name" class="form-control" id="exampleInputEmail1" placeholder="Product Name">
          </div>
          <div class="form-group">
            <label for="exampleInputPassword1">Product Model</label>
            <input type="text" name="pr_code" class="form-control" id="exampleInputPassword1" placeholder="Product Model">
          </div>

          <div class="form-group">
            <label for="exampleInputFile">Product Image 1</label>
            <input type="file" name="userfile" id="exampleInputFile" >

          </div>

          <div class="form-group">
            <label for="exampleInputFile">Product Image 2</label>
            <input type="file" name="userfile2" id="exampleInputFile" >

          </div>




          <button type="submit" class="btn btn-default">Submit</button>
        </form>
    </div>
</div>

My Database

CREATE TABLE `products` (
  `pr_id` int(5) NOT NULL,
  `pr_name` varchar(200) NOT NULL,
  `pr_code` varchar(200) NOT NULL,
  `photo_file` varchar(255) NOT NULL,
  `photo_file2` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Please help me to upload into separate database fields as two individual file

1

1 Answers

4
votes

Use this function and enjoy ;)

public function save() {
  $data = array();

  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'jpg|png';
  $config['max_size'] = 5024;
  $config['encrypt_name'] = true;  

  $this->load->library('upload', $config);

  $data['pr_name'] = $this->input->post('pr_name');
  $data['pr_code'] = $this->input->post('pr_code');

  if (!$this->upload->do_upload('userfile')) {
    $error = array('error' => $this->upload->display_errors());
  } else {
    $fileData = $this->upload->data();
    $data['userfile'] = $fileData['file_name'];
  }

  if (!$this->upload->do_upload('userfile2')) {
    $error = array('error' => $this->upload->display_errors()); 
  } else {
    $fileData = $this->upload->data();
    $data['userfile2'] = $fileData['file_name'];
  }
  // Verify the data using print_r($data); die;
  $result = $this->Product_model->addProducts($data);
  if ($result) {
    $this->load->view('success_view');
  } else {
    $this->load->view('failure_view');
  }
}