5

I want to delete the image not only in database, but in folder too.

This is my database table:

group_id
group_name
group_descrtion
group_picture

This is my controller :

    public function delete_group()
    {
            $group_id = $this->input->post('group_id');
            $group_picture = $this->input->post('group_picture');

            $this->group_model->delete_group($group_id, $group_picture);
            redirect('product_group');
    }

this is my model :

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

                unlink(base_url("uploads/".$group_picture));

                $this->db->delete('product_group', array('group_id' => $group_id));
        }

and this is my view :

 <?=form_open_multipart('product_group/delete_group');?>

   <input type="hidden" class="form-control" placeholder="Group ID" name="group_id">
   <input type="text" class="form-control" placeholder="Group ID" name="group_picture">

  <div class="modal-footer">
     <button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
     <button class="btn btn-success" type="submit">Submit</button>
   </div>
 </form>

I succeed in deleting the data in the database, but the image in the folder are not also be deleted.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TableMan
  • 205
  • 3
  • 6
  • 15

5 Answers5

23
unlink(base_url("uploads/".$group_picture));

Should be

unlink("uploads/".$group_picture);

You need the path, not the url.

Jeremy Jackson
  • 2,247
  • 15
  • 24
3

Use CI constant FCPATH as location of root directory, so in your case that should be:

unlink( FCPATH . "uploads/" . $group_picture );

Notice that FCPATH already has trailing slash.

Tpojka
  • 6,996
  • 2
  • 29
  • 39
1

To delete a file/picture you can use delete_files() function. First load the file helper and then pass the file path as a first parameter in the delete_files() function and true as the second.like-

delete_files($path, true);
Khairul Islam
  • 1,207
  • 1
  • 9
  • 20
1

USE FCPATH in you Delete function

unlink(FCPATH."uploads/".$image);
0

You just need to give only directory name that starts from the folder, not from base_url

unlink("uploads/group/Images/".$group_picture);

Ijaz Ali
  • 60
  • 6