[eluser]Sein Kraft[/eluser]
Hi everyone. I've a simple file uploader wich set the avatar for the user but i want also add an input for set the avatar from url. The question is vallidate and use the url if the user havent selected a file but for that i should remove the avatar callback.
More than a problem is a performance question I think. What I should do?
This my code:
Code:
function _set_avatar(){
$this->form_validation->set_rules('avatar_file', 'Avatar', 'callback__avatar_check');
$this->form_validation->set_rules('avatar_url', 'Avatar', 'trim|min_length[5]');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('users/profile');
}
else
{
//Agregar aviso de error durante la subida del avatar.
$config_avatar['upload_path'] = './temp/';
$config_avatar['allowed_types'] = 'gif|jpg|png';
$config_avatar['overwrite'] = TRUE;
$config_avatar['max_size'] = '100';
$config_avatar['max_width'] = '100';
$config_avatar['max_height'] = '100';
$this->load->library('upload', $config_avatar);
if (!$this->upload->do_upload('avatar_file'))
{
//I suppose that the code for the url should be here
}
else
{
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$this->load->model('mUsers','', TRUE);
$users = $this->mUsers->get_user('id', $this->session->userdata('id'));
$temp_file = './temp/'.$upload_data['file_name'];
$new_file = './accounts/'.$users->user.'/avatar'.$upload_data['file_ext'];
//We delete the olda avatar to create the new one.
unlink($new_file);
rename($temp_file , $new_file);
$user_data['id'] = $users->id;
$user_data['avatar'] = $this->mConfig->get_config('subdomain_accounts').$users->user.'/avatar'.$upload_data['file_ext'];
$this->mUsers->set_avatar($user_data);
redirect('users/profile');
}
}
$this->load->view('footer');
}
/*
- Check if the field of the avatar is not empty.
*/
function _avatar_check(){
if($_FILES['avatar_file']['error'] == 4){
$this->form_validation->set_message('_avatar_check','File to use as avatar is required.');
return FALSE;
}
else{
return TRUE;
}
}