php - Codeigniter replace uploaded image -


i'm using codeigniter's file uploading class uploading user avatars. there way replace user's image file whenever he/she uploads new one? want replace existing avatar newest 1 uploaded.

my image uploading controller

function upload_avatar() { $config['upload_path'] = './uploads/avatars/'; $config['allowed_types'] = 'jpg|png'; $config['overwrite'] = false; //overwrite user avatar $config['encrypt_name'] = true; $config['max_size'] = '200'; //in kb $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $error = $this->upload->display_errors(); $this->session->set_flashdata('error', $error); redirect('/settings/avatar'); } else { $config['image_library'] = 'gd2'; $config['source_image'] = $this->upload->upload_path.$this->upload->file_name; $config['create_thumb'] = false; $config['maintain_ratio'] = false; $config['width'] = 120; $config['height'] = 120; $this->load->library('image_lib', $config); $this->image_lib->crop(); //add image path database $avatar_path = 'uploads/avatars/' . $this->upload->file_name; $user_id = $this->tank_auth->get_user_id(); $this->settings_model->update_avatar($avatar_path, $user_id); $this->session->set_flashdata('success', 'avatar updated!'); redirect('/settings/avatar'); } } 

there public attribute overwrite dictates decision overwrite original file. default, new filename created based on original. here's source upload.php in ci:

/* * validate file name * function appends number onto end of * file if 1 same name exists. * if returns false there problem. */ $this->orig_name = $this->file_name; if ($this->overwrite == false) { $this->file_name = $this->set_filename($this->upload_path, $this->file_name); if ($this->file_name === false) { return false; } } 

so need overwrite working is:

$this->load->library('upload', $config); $this->upload->overwrite = true; 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -