Jump to content


Delete uploaded image when I delete a record


  • Please log in to reply
6 replies to this topic

#1 vnt

vnt

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts

Posted 02 May 2012 - 01:34 PM

Hi,
when I delete a record with an image uploaded, the record is delete but the image is not delete!

How do I delete the image when the record is deleted?

Thenks a lot.
Sorry for my english.
D.

#2 xxaxxo

xxaxxo

    grocery CRUD Expert

  • Members
  • PipPipPip
  • 71 posts
  • LocationBulgaria

Posted 02 May 2012 - 02:02 PM

you use callback_before_delete to call a function which deletes the image

#3 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,114 posts
  • LocationLondon

Posted 02 May 2012 - 06:45 PM

Yeap xxaxxo is right. And I also have an example of how to use the callback_before_delete at http://www.grocerycr...k_before_delete . But again you have to do it manually.
Posted Image

#4 jrowe

jrowe

    Member

  • Members
  • PipPip
  • 23 posts

Posted 05 May 2012 - 02:17 AM

So I am trying to implement this as well.. here is what I have but it does not delete the file , it deletes the entry from the table but it doesnt update the crud ui. in other words i have to refresh the page to see it remove from the table. i use the standard path for uploads that grocerycrud comes with. here is what i have..



public function editevents(){


if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');

} else {

$this->load->library('grocery_CRUD');
$this->grocery_crud->set_table('event');
$this->grocery_crud->set_theme('datatables');
$this->grocery_crud->required_fields('eventtype','fileurl','header','description','date');

$this->grocery_crud->set_field_upload('fileurl','assets/uploads/files');
$this->grocery_crud->callback_after_upload(array($this,'example_callback_after_upload'));
$this->grocery_crud->callback_before_delete(array($this,'delete_event_pic_before_delete'));
        $output = $this->grocery_crud->render();

        $this->_events_output($output);    

}

}

public function delete_event_pic_before_delete($primary_key)
{
   $user= $this->db->where('eventid',$primary_key)->get('event')->row();
   
 $url=base_url('assets/uploads/files').'/'.$user->fileurl;
unlink($url);
return true;

}
with out trying to add the delete before call back this worked perfectly. please help, thanks

#5 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,114 posts
  • LocationLondon

Posted 05 May 2012 - 09:53 AM

Thanks to share your code. I think it is better to do:

$url= 'assets/uploads/files/'.$user->fileurl;

than

$url=base_url('assets/uploads/files').'/'.$user->fileurl;

So you will have:

public function delete_event_pic_before_delete($primary_key)
{
   $user= $this->db->where('eventid',$primary_key)->get('event')->row();
  
$url= 'assets/uploads/files/'.$user->fileurl;
unlink($url);
return true;
}

Posted Image

#6 jrowe

jrowe

    Member

  • Members
  • PipPip
  • 23 posts

Posted 05 May 2012 - 04:09 PM

Thank you web-johnny, it is working now. I love your project and the work you have done!! I am only a hobby web dev but you make working with Codeigniter muccch more enjoyable!!

#7 Deeps

Deeps

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 10 March 2016 - 04:27 AM

image not delete from database when update time..plz help






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users