0

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

  • in your model code `unlink("./gambar/.....` why are you defining the path from root directory with `./`? maybe this is causing the permission problem? try put the folder in your project directory or try`..` to access the right directory. – saur Dec 12 '18 at 05:12
  • also make sure that the folder that you are deleting from has 777 permission – saur Dec 12 '18 at 05:13
  • use FCPATH or APPPATH to get your file – Danish Ali Dec 12 '18 at 05:37

2 Answers2

0

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

Sherif Salah
  • 2,085
  • 2
  • 9
  • 21
0

Thanks for all the answer guys, it's already worked. Here's my complete code, in case others may need this in the future

View :

<tbody>
                            <?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 } ?>
                        </tbody>

Controller :

function hapus($a,$b){
    $group_id = $this->input->post('ID');
    $group_picture = $this->input->post('namafile');
    unlink(FCPATH."/gambar/hasil/data_trainingtanpakotak/".$a."/".$b);
    $this->m_admin->hapus_data($group_id, $a."/".$b);
    redirect('datatraining');
}

Model :

function hapus_data($group_id, $group_picture){
    $this->db->where('namafile', $group_picture);

    $this->db->delete('datatraining', array('namafile' => $group_picture));
}
  • giving that folder a 777 permission is a really bad idea. The rest I can agree with. The folder just need to be 775, what you have to make sure is that the ownership of that folder is the correct one. Like if you're using apache the ownership should be something like www-data:YOURUSERNAME this way apache can add or delete things from that folder and your not giving writing access to everyone. – marcogmonteiro Dec 12 '18 at 20:42