Manee
Manee

Reputation: 1

cakephp remove image file from webroot/img folder after deleting record from table

I am trying to remove the image file from my webroot/img/uploads folder. However I have succeeded to delete the entries from my mysql db tables but i need to remove the hardcopy as well from the webroot folder...

the following is my controller function for delete photo

  public function delete_photo($id){
        $con['Photo.id'] = $id;
        $the_photo = $this->Photo->find('first',array('conditions' => $con));
        //var_dump($the_photo);
        //echo $the_photo['Photo']['image_name'];
        $this->Photo->delete($id);
        function afterDelete($id)
        {
        $file = new File(WWW_ROOT .'/img/uploads/'. $the_photo['Photo']['image_name'],false, 0777);
        if($file->delete()){
            echo "File deleted";
        }
        else{
            echo "Deletion failed!!";
        }                
        }
        $this->autoRender = false;

  }

Upvotes: 0

Views: 3943

Answers (2)

Ayush Pant
Ayush Pant

Reputation: 392

Try This

You are using function in Function :

try {
    $data = $this->Document->find('all',array('conditions'=>array('id'       => $id)));
    $f = $data[0]['Document']['doc_file'];
    $documents = $this->Document->delete($id);
    $file = new File(WWW_ROOT .$f,false, 0777);
    if($file->delete()){
        $this->Session->setFlash('Deleted Successfully', 'default',      array('class' => 'succuss'));
        $this->redirect('add');
    } else {echo 'files not deleted';}}
} catch (Exception $e){
    // Do something with an exception
}

Upvotes: 4

Akshay Sharma
Akshay Sharma

Reputation: 1112

You can simple delete file through php unlink function.

        $data = $this->Document->find('first',array('conditions'=>array('id'      => $id)));
        $f = $data['Document']['doc_file'];
        @unlink(YOUR_ROOT_PATH.$f);

Upvotes: 2

Related Questions