Search code examples
phpcodeigniterfile-upload

Multiple image upload with CodeIgniter


I am new user to using code igniter in my project, I am facing one problem while uploading multiple files but the last one only insert to all image three images field.

my controller is:

function products()
    {
     date_default_timezone_set("Asia/Kolkata");
     $config['upload_path'] = './resources/images/products/';
     $config['allowed_types']        = 'gif|jpg|png';
     $config['max_size']             = 1000;
     $config['max_width']            = 1024;
     $config['max_height']           = 768;
     $this->load->library('upload', $config);
     $this->upload->do_upload('userfile');
        $data = array('prod_image' => $this->upload->data(), 
             'prod_image1' => $this->upload->data(),
              'prod_image2' => $this->upload->data());
    $product_image=$data['prod_image']['file_name']; 
    $product_image1=$data['prod_image1']['file_name'];
    $product_image2=$data['prod_image2']['file_name'];

        $data = array(
                'name' => $this->input->post('pd_name'),
                'prod_image' => $product_image,
                'prod_image1' => $product_image1,
                'prod_image2' => $product_image2,
                'created_time' => date('Y-m-d H:i:s'));

        // insert form data into database
        $result_set= $this->tbl_products_model->insertUser($data);

    }       

my view part is:

<input class="form-control" name="pd_name"type="text"/>
<input type="file"  class="file_upload2" name="userfile"/> //1
<input type="file" class="file_upload2" name="userfile"/> //2
<input type="file" class="file_upload2" name="userfile"/>//3

Please help how to insert 3 images.

my datad base like

===========================================
id|name|prod_image|prod_image1|prod_image2|
===========================================
 1|ard|           |           |          |
============================================

Solution

  • Html :

    <input type="file" name="userfile[]" multiple="multiple">
    

    PHP :

    <?php
    public function products()
    {       
        $this->load->library('upload');
        $dataInfo = array();
        $files = $_FILES;
        $cpt = count($_FILES['userfile']['name']);
        for($i=0; $i<$cpt; $i++)
        {           
            $_FILES['userfile']['name']= $files['userfile']['name'][$i];
            $_FILES['userfile']['type']= $files['userfile']['type'][$i];
            $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
            $_FILES['userfile']['error']= $files['userfile']['error'][$i];
            $_FILES['userfile']['size']= $files['userfile']['size'][$i];    
    
            $this->upload->initialize($this->set_upload_options());
            $this->upload->do_upload('userfile');
            $dataInfo[] = $this->upload->data();
        }
    
        $data = array(
            'name' => $this->input->post('pd_name'),
            'prod_image' => $dataInfo[0]['file_name'],
            'prod_image1' => $dataInfo[1]['file_name'],
            'prod_image2' => $dataInfo[2]['file_name'],
            'created_time' => date('Y-m-d H:i:s')
         );
         $result_set = $this->tbl_products_model->insertUser($data);
    }
    
    private function set_upload_options()
    {   
        //upload an image options
        $config = array();
        $config['upload_path'] = './resources/images/products/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']      = '0';
        $config['overwrite']     = FALSE;
    
        return $config;
    }
    ?>