php - how do you handle a double image file when it`s edited
when i edit the image file, instead it double files in the folder, does anyone know how to handle it?
Controllers
**public function editHome()
{
$config['upload_path'] = './upload/images/slide/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
//edit
$id_home = $this->input->post('id_home1', true);
$data['dm'] = $this->db->get_where('tb_home', ['id_home' => $id_home])->row_array();
if (!$this->upload->do_upload('gambar_home')) {
echo "Gagal Update";
} else {
$dd = array('upload_data' => $this->upload->data());
$dm = [
'judul_home' => $this->input->post('judul_home'),
'gambar_home' => $dd['upload_data']['file_name'],
'desc_home' => $this->input->post('desc_home'),
'status_home' => $this->input->post('status_home')
];
$this->db->where('id_home', $this->input->post('id_home', true))->update('tb_home', $dm);
}
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert"> Berhasil Update !</div>');
redirect('admin/content/home');
}**
Answer
Solution:
By default, codeigniter file upload library dont replace the uploaded file with the same name, instead file is saved with [filename]1.jpg
. If file name is same, you can replace it use:
$config['overwrite'] = true; //set true to overwrite the data
$config['upload_path'] = './upload/images/slide/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
Source