Eslam Mohamed
Eslam Mohamed

Reputation: 21

i can't delete image and i get unlink

I can not delete an article or image in case I delete anything that shows the page Whoops

if(!function_exists('image_delete')){
    function image_delete($dir){
        unlink(public_path().'/'.$dir);
    }
}

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Article $article)
    {
        image_delete($article->image); // its helper function
        $article->delete();
        return back()->with('success','تم حذف المقال !!');
    }

this my problem this my problem

Upvotes: 1

Views: 203

Answers (2)

Natvarsinh Parmar - bapu
Natvarsinh Parmar - bapu

Reputation: 1138

For example, your article image store in public/article_images directory then code as per below.

In helper:

if(!function_exists('image_delete')){
    function image_delete($filename){
       $file = public_path().'/article_images/'.$filename;

       if(file_exists($file)){
          unlink($file);
       }
    }
}

Upvotes: 1

Kmg Kumar
Kmg Kumar

Reputation: 616

Before, you delete any files or folder please check the file or folder exists or not. change your helper code like this. here file_exist function check you dir or files exists or not.

if(!function_exists('image_delete')){
    function image_delete($dir){
      if(file_exists($dir))
        unlink(public_path().'/'.$dir);
    }
}

Upvotes: 1

Related Questions