⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Delete uploaded image when I delete a record



vnt

vnt
  • profile picture
  • Member

Posted 02 May 2012 - 13: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.

xxaxxo

xxaxxo
  • profile picture
  • Member

Posted 02 May 2012 - 14:02 PM

you use callback_before_delete to call a function which deletes the image

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 02 May 2012 - 18:45 PM

Yeap [member='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.

jrowe

jrowe
  • profile picture
  • Member

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

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

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;
}

jrowe

jrowe
  • profile picture
  • Member

Posted 05 May 2012 - 16: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!!

Deeps

Deeps
  • profile picture
  • Member

Posted 10 March 2016 - 04:27 AM

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