Is there any chance that I can create a custom function for upload? I have 2 function which are insert_content() and insert_officials. In that function there is a code for uploading image. How will I do that since they have the same code of config.
public function insert_content() { $data = array('success' => false, 'messages' => array()); $name = array('post_title','post_category','post_description'); $label = array('Title','Category','Description'); $verify = 'trim|required'; $this->validate($name[0], $label[0], $verify); $this->validate($name[1], $label[1], $verify); $this->validate($name[2], $label[2], $verify); $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>'); if($this->form_validation->run()) { $data['success'] = true; } else { foreach($_POST as $key => $value) { $data['messages'][$key] = form_error($key); } echo json_encode($data); exit; } $date = date('Y'); $month = date('m'); $upload_config = array ( 'upload_path' => './assets/uploads/'.$date.'/'.$month, 'allowed_types' => 'jpg|png|jpeg', 'max_size' => '2048', 'max_width' => '1536', 'max_height' => '1024' ); $this->load->library('upload', $upload_config); if(!is_dir($upload_config['upload_path'])) { mkdir($upload_config['upload_path'], 0777, TRUE); } if($this->upload->do_upload('post_image')) { $data = array('upload_data' => $this->upload->data()); $image = $data['upload_data']['file_name']; $data = array ( $name[0] => $this->pass($name[0]), $name[1] => $this->pass($name[1]), $name[2] => $this->pass($name[2]), 'post_image' => $image ); $this->model->CreateContent($data); } } //Officials public function insert_officials() { $data = array('success' => false, 'messages' => array()); $name = array('official_fname','official_mname','official_lname','official_nickname','official_contact','official_position','official_barangay','official_address'); $label = array('First Name','Middle Name','Last Name','Nickname','Contact','Position','Barangay','Address'); $verify = 'trim|required'; $number = 'required|regex_match[/^[0-9]{11}$/]'; $this->validate($name[0], $label[0], $verify); $this->validate($name[1], $label[1], $verify); $this->validate($name[2], $label[2], $verify); $this->validate($name[3], $label[3], $verify); $this->validate($name[4], $label[4], $number); $this->validate($name[5], $label[5], $verify); $this->validate($name[6], $label[6], $verify); $this->validate($name[7], $label[7], $verify); $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>'); if($this->form_validation->run()) { $data['success'] = true; } else { foreach($_POST as $key => $value) { $data['messages'][$key] = form_error($key); } echo json_encode($data); exit; } $date = date('Y'); $month = date('m'); $upload_config = array ( 'upload_path' => './assets/uploads/'.$date.'/'.$month, 'allowed_types' => 'jpg|png|jpeg', 'max_size' => '2048', 'max_width' => '2048', 'max_height' => '1024' ); $this->load->library('upload', $upload_config); if(!is_dir($upload_config['upload_path'])) { mkdir($upload_config['upload_path'], 0777, true); } if (!$this->upload->do_upload('official_image')) { $data = array('error' => $this->upload->display_errors('<span class="ti-remove"></span> ')); } else { $data = array('upload_data' => $this->upload->data()); $image = $data['upload_data']['file_name']; $data = array ( $name[0] => $this->pass($name[0]), $name[1] => $this->pass($name[1]), $name[2] => $this->pass($name[2]), $name[3] => $this->pass($name[3]), $name[4] => $this->pass($name[4]), $name[5] => $this->pass($name[5]), $name[6] => $this->pass($name[6]), $name[7] => $this->pass($name[7]), 'official_image' => $image ); $this->model->CreateOfficials($data); } }
Advertisement
Answer
Just make a function and put it in a model or something:
function upload($fieldname); $date = date('Y'); $month = date('m'); $upload_config = array ( 'upload_path' => './assets/uploads/'.$date.'/'.$month, 'allowed_types' => 'jpg|png|jpeg', 'max_size' => '2048', 'max_width' => '1536', 'max_height' => '1024' ); $this->load->library('upload', $upload_config); if(!is_dir($upload_config['upload_path'])) { mkdir($upload_config['upload_path'], 0777, TRUE); } if($this->upload->do_upload($fieldname)) { return $this->upload->data(); } return false; }
Usage:
$this->load->model('somemodel'); $imageup = $this->somemodel->upload('post_image'); if ($imageup) { // image uploaded succesfully echo $imageup['file_name']; } else { // upload failed // you should be able to also access error echo $this->upload->display_errors(); }