猿问

我无法删除图像,我无法取消链接

如果我删除任何显示该页面的内容,我将无法删除文章或图片


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','تم حذف المقال !!');

    }

这是我的问题

白板的微信
浏览 177回答 2
2回答

幕布斯6054654

在删除任何文件或文件夹之前,请检查文件或文件夹是否存在。像这样更改您的助手代码。这里 file_exist 函数检查你的目录或文件是否存在。if(!function_exists('image_delete')){    function image_delete($dir){      if(file_exists($dir))        unlink(public_path().'/'.$dir);    }}

鸿蒙传说

例如,您的文章图像存储在 public/article_images 目录中,然后按照以下代码进行编码。在帮手中:if(!function_exists('image_delete')){    function image_delete($filename){       $file = public_path().'/article_images/'.$filename;       if(file_exists($file)){          unlink($file);       }    }}
随时随地看视频慕课网APP
我要回答