Saturday 28 September 2013

Codeigniter validate_upload library can't upload file

Codeigniter validate_upload library can't upload file

I was referred to @raheel shan's thread on this page and use this
My_Upload extended library to have
if(!$this->upload->validate_upload('photo')) validation of input file, the
validation is work fine if user try to upload invalid file type and the
message is just shown in the right place.
Somehow, I can't upload image file to its folder and the filename is
unable to save into table, there was works pretty to upload the image file
and saved to table before I use this library.
Can someone please help with figure what's wrong in my code?
Controller:
public function index()
{
if(!$this->tank_auth->is_logged_in()) {
redirect('/auth/login');
}else{
$user_id = $this->session->userdata('user_id');
$profile = $this->users->get_profile_by_id($user_id);
$checked = $this->input->post('remove');
//$data['errors'] = array();
$this->form_validation->set_rules('name', 'Name',
'trim|required|xss_clean|min_length[5]|max_length[30]');
$this->form_validation->set_rules('country', 'Country',
'trim|required|xss_clean');
$this->form_validation->set_rules('website', 'Website',
'trim|xss_clean|max_length[30]');
//http://www.jeswin.com/professional/programming/codeigniter-uploading-an-image
if(isset($_FILES['photo']) AND !empty($_FILES['photo']['name'])) {
$this->form_validation->set_rules('photo', 'Photo',
'trim|callback_valid_upload');
if($this->upload->do_upload('photo')){
// image resizing
$config['source_image'] =
$this->upload->upload_path.$this->upload->file_name;
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$this->load->library('image_lib', $config); //codeigniter
default function
if(!$this->image_lib->resize()){
$this->session->set_flashdata('upload_msg',
$this->image_lib->display_errors());
}else{
$image_data = $this->upload->data();
$photo_to_table = $image_data['file_name'];
}
}
}else{
if((int) $checked == 1){
unlink('./uploads/'.$profile->photo);
$photo_to_table = '';
}else{
$photo_to_table = $profile->photo;
}
//$photo_to_table = ((int) $checked == 1) ? '' : $profile->photo;
}
if($this->form_validation->run()) { // validation ok
if(!is_null($data = $this->tank_auth->update_user(
$this->form_validation->set_value('name'),
$this->form_validation->set_value('country'),
$this->form_validation->set_value('website'),
$photo_to_table
))) { // success
$this->session->set_flashdata('msg', 'Profile has been
updated');
redirect(current_url());
}
}else{
$errors = $this->tank_auth->get_error_message();
foreach($errors as $k => $v) $data['errors'][$k] =
$this->lang->line($v);
}
//blah blah..//load view
}
public function valid_upload()
{
$user_id = $this->session->userdata('user_id');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '500';
$config['file_name'] = $user_id.'_'.strtotime('now');
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
//$this->load->library('upload', $config);
$this->upload->initialize($config);
if(!$this->upload->validate_upload('photo'))
{
$this->form_validation->set_message('valid_upload',
$this->upload->display_errors());
return FALSE;
}else{
return TRUE;
}
}
}
Thanks.

No comments:

Post a Comment