Multiple picture is not uploading , always gets the first picture

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

1 answer

  • answered 2018-11-08 10:42 AbdulAhmad Matin

    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);