codeigniterupdatesmulti-upload

Update Selected file on Multiple Upload CodeIgniter


I've write the codeigniter for three upload file. And when i update one or two, the third will be overwrite data that i've upload before with blank in db. how to make it update only the file that i need to . i've try if !empty $_FILES but my head start smoky :(

 <input type="file" class="form-control"  name="userfile[]">Front
 <input type="file" class="form-control"  name="userfile[]">Back
 <input type="file" class="form-control"  name="userfile[]">Side

this my controller

public function prosesUpdate2(){
$data = $this->input->post('id');
    $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();
        $dataInfo[] = $this->upload->data();
    }
    $files = array(
        'front'       => $dataInfo[0]['file_name'],
        'back'       => $dataInfo[1]['file_name'],
        'side'       => $dataInfo[2]['file_name']
        // 'userdatecreate'    => date('Y-m-d H:i:s')
        );
        $result_set = $this->update_building->db_update($files, $data);
        $this->session->set_flashdata('file_success', 'Upload File Success!');

}

the model

public function db_update($data,$id)
{
$this->db->where('id', $id);       
$this->db->update('allbuidingdata', $data);
}

Solution

  • Please check the posted files before the update.

    Slightly modify your code.

    public function prosesUpdate2(){
      $data = $this->input->post('id');
      $this->load->library('upload');
    
      $dataInfo = array();
      $files = $_FILES;
      $files_to_update = array();
      $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();
        $dataInfo[] = $this->upload->data();
      }
      $files_to_update= array(
         'userdatecreate'    => date('Y-m-d H:i:s')
      );
      if($dataInfo[0]['file_name']){
         $files_to_update['front'] = $dataInfo[0]['file_name'];
      }
      if($dataInfo[1]['file_name']){
         $files_to_update['back'] = $dataInfo[1]['file_name'];
      }
      if($dataInfo[2]['file_name']){
         $files_to_update['side'] = $dataInfo[2]['file_name'];
      }
      $result_set = $this->update_building->db_update($files_to_update, $data);
      $this->session->set_flashdata('file_success', 'Upload File Success!');
    }