I am working with codeigniter, i want to delete a file from a folder and also delete related data from database. But how to ensure that either both will be done successfully otherwise none of them will took place.
Following code is my approach towards it, but i am concern about the case like, file deleted successfully but for some reason database operation was unsuccessful. Is there any better approach for this?
if(unlink($path)){
$this->admin_model->delete_data($id,$db);
}
I'm sorry with my answers, but with my exp to work with CI for 5 months. I'll use this method: First, function delete_data($id,$db) must be return an effective_rows like:
public function delete_data($id,$db)
{
// your code delete
return $this->db->affected_rows();
}
Then edit a little bit of your code:
if($this->admin_model->delete_data($id,$db) > 0)
{
if(unlink($path)) {
return true;
}else{
return false;
}
}
Hope it help. Sorry about my bad english.