1

View :

<div class="col-md-6">
        <div class="form-group">              
                <input type = "file" class="form-control"  name = "userfile" id="userfile"  size = "20" />
        </div>
 </div>
 <div class="col-md-6">          
        <div class="form-group">             
                <input type = "file" class="form-control"  name = "userfile1" id="userfile1"  size = "20" />
        </div>
 </div>

Controller :

 $this->do_upload_landscape($data['package']['pID'] ,$this->input->post('userfile'));
 $this->do_upload_thumb($data['package']['pID'] ,$this->input->post('userfile1'));

function to upload first picture

public function do_upload_landscape($member_id,$imageland)
        {
            $data['package'] = $this->Package_model->get_package_for_edit($member_id);
            $new_name = $data['package']['pID'].$data['package']['packgeID'] ;
            $config['file_name'] = $new_name;
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            $this->load->library('upload', $config);
            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
                $data['_view'] = 'package/add';
                $this->load->view('admin/layouts/main',$data,$error);
            }
            else
            {
                $data = array('upload_data' => $this->upload->data($imageland));
                $file_data= $data['upload_data'];
                $params = array(
                    'files' => site_url().'uploads/'.$file_data['file_name']
                );
               $result= $this->Package_model->update_package($member_id,$params);
            }
        }

Function to upload 2nd picture

public function do_upload_thumb($member_id,$thumb)
        {   
            $data1['package'] = $this->Package_model->get_package_for_edit($member_id);
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            $this->load->library('upload', $config);
            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
                $data1['_view'] = 'package/add';
                $this->load->view('admin/layouts/main',$data1,$error);
            }
            else
            {
                $data1 = array('upload_data' => $this->upload->data($thumb));
                $file_data= $data1['upload_data'];
                $params = array(

                    'files_thumb' => site_url().'uploads/'.$file_data['file_name']
                );
                $result= $this->Package_model->update_package($member_id,$params);

            }
        } 

Problem : Both times, I'm getting the same picture. Picture value from view page comes fine , but when uploding , it always gets the value of first picture. In db , if the first picture name is 56R45 , then the 2nd picture name is 56R451, that means same pictured added two times . Thanks

5hakir
  • 119
  • 10
  • Possible duplicate of [Multiple files upload in Codeigniter](https://stackoverflow.com/questions/20113832/multiple-files-upload-in-codeigniter) – Bergin Nov 08 '18 at 11:15

2 Answers2

0

You need to specify the filename in the $this->upload->do_upload() . you should pass the name of the input element. e.g

<input type = "file" class="form-control"  name = "userfile" id="userfile"  size = "20" />

then you must call the $this->upload->do_upload() with the name of the file like this $this->upload->do_upload('userfile'). Same for the second file input element.

Tip: you should refactor your file uploading code. Instead of using two separate function you can make one function and call it any times you want.

Ropali Munshi
  • 2,757
  • 4
  • 22
  • 45
-1

you must initialize the upload file and add this

$this->upload->initialize($config);

after of

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

in both function like this

$this->load->library('upload', $config);
$this->upload->initialize($config);
AbdulAhmad Matin
  • 1,047
  • 1
  • 18
  • 27