title: Codeigniter replace uploaded image
body: <p>I'm using Codeigniter's <a href="http://codeigniter.com/user_guide/libraries/file_uploading.html" rel="noreferrer">File Uploading Class</a> 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.</p>
  
  <p>My image uploading controller</p>
  
  <pre><code>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-&gt;load-&gt;library('upload', $config);
  
          if ( ! $this-&gt;upload-&gt;do_upload())
          {
              $error = $this-&gt;upload-&gt;display_errors(); 
  
              $this-&gt;session-&gt;set_flashdata('error', $error);
  
              redirect('/settings/avatar');
          }
          else
          {                
              $config['image_library'] = 'gd2';
              $config['source_image'] = $this-&gt;upload-&gt;upload_path.$this-&gt;upload-&gt;file_name;
              $config['create_thumb'] = FALSE;
              $config['maintain_ratio'] = FALSE;
              $config['width'] = 120;
              $config['height'] = 120;
  
              $this-&gt;load-&gt;library('image_lib', $config); 
  
              $this-&gt;image_lib-&gt;crop();
  
              //Add image path to database
              $avatar_path = 'uploads/avatars/' . $this-&gt;upload-&gt;file_name;
              $user_id = $this-&gt;tank_auth-&gt;get_user_id();
              $this-&gt;Settings_model-&gt;update_avatar($avatar_path, $user_id);
  
              $this-&gt;session-&gt;set_flashdata('success', 'Avatar updated!');
  
              redirect('/settings/avatar');
          }
      }
  </code></pre>