Gimana caranya menghapus file menggunakan CodeIgniter?

Ditanyakan pada tanggal 5 February 2019 oleh Ridwan Renaldi
public function updateImage($path, $table, $where, $post)
{
    if(!empty($_FILES['image']['name'])){
        $oldName                        = $this->input->post('image')['content'];
        $config['upload_path']          = './resource/images/'.$path.'/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 1000;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;

        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('image'))
        {
            return array("status" => "error", "desc" => $this->upload->display_errors());
        }
        else
        {
            $target = base_url('resource/images/'.$path.'/'.$oldName);
            unlink($target);

            $this->updateSetting($table, $where, $post);
            return array("status" => "success", "desc" => $this->upload->data(), "target"=>$target);
        }

    } else {
        return array("status" => "empty", "desc" => "<p>Image not found</p>");
    }
}

Sudah coba pake unlink tetep gabisa ke hapus, gimana solusinya? $target saya cek juga sudah benar

2 JAWABAN / 2 KOMENTAR

Generic placeholder image
Ahmad Oriza menjawab pada tanggal 7 February 2019

Ini dikarenakan salah parameter pada saat unlink gambar. Harusnya gunakan alamat gambar, bukan menggunakan base URL

Coba diganti menjadi seperti ini mas :

public function updateImage($path, $table, $where, $post)
{
    if(!empty($_FILES['image']['name'])){
        $oldName                        = $this->input->post('image')['content'];
        $config['upload_path']          = './resource/images/'.$path.'/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 1000;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;

        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('image'))
        {
            return array("status" => "error", "desc" => $this->upload->display_errors());
        }
        else
        {
            $target = './resource/images/'.$path.'/'.$oldName;
            unlink($target);

            $this->updateSetting($table, $where, $post);
            return array("status" => "success", "desc" => $this->upload->data(), "target"=>$target);
        }

    } else {
        return array("status" => "empty", "desc" => "<p>Image not found</p>");
    }
}
Generic placeholder image
Patar Ebenezer Siahaan menjawab pada tanggal 5 February 2019

Coba dijelaskan kasusnya seperti apa mas.. kalo begini, agak kurang paham. (belum bisa menjadi dukun) hehe