I am working on a basic **blog application** in Codeigniter 3.1.8 and Bootstrap 4.

Every post has a main image hence, there is a `post_image` column in the `posts` table.

I have this problem when a post's image is replaced with a *new image*, that has the *same (file)name* as the old one: the image filename is incremented - mypic.jpg becomes mypic1.jpg - but the *filename string* is not updated in the `posts` table. 

The code is this and I believe I have identified the source of the problem (commented):

```
if(!$this->upload->do_upload()){
    $errors = array('error' => $this->upload->display_errors());
    //Keep the current image name in the posts table
    //If no new image is loaded
    $post_image = $this->input->post('postimage');
} else {
    $data = array('upload_data' => $this->upload->data());
    // This line is the source of the problem
    $post_image = $_FILES['userfile']['name'];
}

```

I don't know how to increment it properly or even if increment is the best solution.

**The question:** What is the most robust (reliable) way to solve this problem?

---

**EDIT:**

For consistency, I should probably update the initial (when the post is *created*) upload code:

```
if (!$this -> upload -> do_upload()) {
    $errors = array('error' => $this -> upload -> display_errors());
    $post_image = 'default.jpg';
} else {
    $data = array('upload_data' => $this -> upload -> data());
    $post_image = $_FILES['userfile']['name'];
}

```