unlink(202104241126Dell_Logo.png):没有那个文件或目录

问题描述

功能删除品牌但照片仍保存在public/upload/brands/文件夹中

我想删除它时也会从文件夹中删除照片。

public function deleteBrand($request){

$deletelogo = $this->brandyModel::where('id',$request->id)->first();

$logo = $deletelogo->logo;
if (is_file($logo)){
unlink(public_path('upload/brands'.$logo));
}
$deletelogo->delete();
$notificat = array(
    'message' => 'Brand Deleted Successfully','alert-type' => 'success',);
return redirect()->back()->with($notificat);

}

解决方法

我认为只是打字错误,让我重构您的代码

public function deleteBrand($request){

$deletelogo = $this->brandyModel::where('id',$request->id)->first();

$logo = $deletelogo->logo;
$url = public_path('view/upload/brands/'.$logo); // Take a look at this Url,you might wanna change it
if (file_exists($url)){
     unlink($url);
}
$deletelogo->delete();
$notificat = array(
    'message' => 'Brand Deleted Successfully','alert-type' => 'success',);
return redirect()->back()->with($notificat);
,

感谢所有尝试帮助。我解决了它路径应该在“”之间,

喜欢这段代码,现在效果很好,

f(return_const())