Codeigniter replace uploaded image

22,085

Solution 1

There is a public attribute overwrite that dictates the decision to overwrite the original file. By default, a new filename is created based on the original. Here's the source from Upload.php in CI:

/*
 * Validate the file name
 * This function appends an number onto the end of
 * the file if one with the same name already exists.
 * If it returns false there was a 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 all you need to do to get the overwrite working is:

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

Solution 2

Simple set "overide" be true in your config.

$this->upload->initialize(array(
            "upload_path"=>$path,
            "allowed_types"=>"jpg|png|jpeg",
            "overwrite"=>true
        ));

Solution 3

did you try to change

$config['overwrite'] = FALSE;

to

$config['overwrite'] = TRUE;
Share:
22,085
CyberJunkie
Author by

CyberJunkie

Updated on July 05, 2022

Comments

  • CyberJunkie
    CyberJunkie almost 2 years

    I'm using Codeigniter's File Uploading Class for uploading user avatars. Is there a way to replace a user's image file whenever he/she uploads a new one? I want to replace an existing avatar with the newest one 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 to 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');
            }
        }
    
    • drfranks3
      drfranks3 almost 12 years
      Have you tried setting this line $config['overwrite'] = FALSE; //overwrite user avatar to TRUE?
  • CyberJunkie
    CyberJunkie almost 12 years
    Thanks! For the replace to work I reckon that I'll have to give each avatar image a unique name like the user's username. This way anything a user uploads replaces what's already stored.
  • Wouter Vanherck
    Wouter Vanherck over 5 years
    A good alternative compared to $this->upload->overwrite = true;