Search code examples
phpimagecodeigniterdelete-fileunlink

Permission Denied when trying to delete image using unlink in codeigniter


i've been following the answer on this thread CodeIgniter : How to delete uploaded image from folder , but when i pressed the delete link, my program process the function, but the data not deleted.

Here's my database table My table name's datatraining

This is my controller code

function hapus(){
    $group_id = $this->input->post('ID');
    $group_picture = $this->input->post('namafile');
    $this->m_admin->hapus_data($group_id, $group_picture);
    redirect('datatraining');
}

This is my model code

function hapus_data($group_id, $group_picture){
    $this->db->where('ID', $group_id);
    unlink("./gambar/hasil/data_trainingtanpakotak/".$group_picture);
    $this->db->delete('datatraining', array('ID' => $group_id));
}

This is my view code

<?php 
                            foreach($datatraining as $u){ 
                                ?>
                                <tr>
                                    <td><?php echo anchor('datatraining/hapus/'.$u->namafile,'Hapus'); ?></td>
                                    <td><?php echo $u->ID ?></td>
                                    <td><img height="50" width="50" src="<?php echo base_url().'gambar/hasil/data_trainingtanpakotak/'.$u->namafile; ?>"></td>
                                    <td><?php echo $u->r ?></td>
                                    <td><?php echo $u->g ?></td>
                                    <td><?php echo $u->b ?></td>
                                    <td><?php echo $u->Label ?></td>
                                </tr>
                            <?php } ?>

Thanks for your help guys


Solution

  • First of all give that folder a permission of 0777 and use the fullpath with unlink like this:

    `unlink(FCPATH."gambar".DIRECTORY_SEPARATOR."hasil".DIRECTORY_SEPARATOR."data_trainingtanpakotak".DIRECTORY_SEPARATOR.$group_picture);`
    

    I've used FCPATH here supposingly that the dir you are deleting from is in your FCPATH