Hey guys,
I've created the following functions which do the following:
1) Creating a dir in server with a unique id,
2) encrypting file name
3) resizing the photo
4) uploading the data in db.
My problem is that i can't get the file name in the db, even though the dir folder is created and the other data are passed. My guess is that i messed up the upload image variables a bit....
Any ideas or suggestions on that (how to solve it or making the code more clean) ?!
Thank you!!
Controller
Code:
public function found(){
if ($this->session->userdata('logged_in') == true) {
$user_id = $this->session->userdata('user_id');
$data['user'] = $this->user_model->get_user_data($user_id);
}
$this->form_validation->set_rules('petTitle_found', 'Entry title', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('pets/found', $data);
} else {
$pet_entry_id = random_string('numeric', 6);
//Image uploading
$config['encrypt_name'] = TRUE;
$config['upload_path'] = './assets/img/pets' . $pet_entry_id;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '204800';
$config['max_width'] = '2048 ';
$config['max_height'] = '2048';
$image_data = array();
$this->load->library('upload', $config);
if ( !is_dir('./assets/img/pets/' . $pet_entry_id) ) {
mkdir('./assets/img/pets/' . $pet_entry_id, 0777, true);
} else {
}
if (!$this->upload->do_upload('userfile')) {
$errors = array('error' => $this->upload->display_errors());
$config['file_name'] = 'default_petavatar.png';
} else {
//store the file info
$image_data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $image_data['full_path'];
$config['maintain_ratio'] = TRUE;
$config['width'] = 550;
$config['height'] = 450;
$this->load->library('image_lib', $config);
if (!$this->image_lib->resize()) {
$this->handle_error($this->image_lib->display_errors());
}
}
$this->pet_model->create_found_pet($pet_entry_id);
redirect('pets');
}
}
Model
Code:
public function create_found_pet($pet_entry_id){
if ($this->session->userdata('logged_in') == true) {
$user_id = $this->session->userdata('user_id');
} else {
$data_user = array(
'screen_name' => $this->input->post('person_name_found'),
'email' => $this->input->post('person_email_found')
);
$this->db->insert('users', $data_user);
$user_id = $this->db->insert_id();
}
$pet_entry = convert_accented_characters($this->input->post('petTitle_found'));
$pet_entry_slug = url_title($pet_entry);
$data_pet = array(
'pet_entry_title' => $this->input->post('petTitle_found'),
'pet_entry_id' => $pet_entry_id,
'category_id' => $this->input->post('petCategory_found'),
'pet_color' => $this->input->post('petColor_found'),
'pet_descr' => $this->input->post('petMoreInfo_found'),
'pet_photo' => $this->upload->data('file_name'),
'pet_entry_slug' => $pet_entry_slug,
'pet_status' => 2,
'user_id' => $user_id
);
return $this->db->insert('pets', $data_pet);
}
//Life motto
if (sad() == true) {
sad().stop();
develop();
}