从 public 直接和数据库中删除上传的文件
Delete uploaded ffile from both public dir and database
我正在尝试从 public 目录和数据库中删除上传的图片。我该怎么做?
我只能从 public 目录中删除,但我不知道如何从数据库中删除。谁能告诉我该怎么办 非常感谢
这是我用来从 public 目录中删除图片的控制器:
public function destroy($id)
{
$image = DB::table('user_images')->where('id', $id)->first();
$file= $image->name;
$filename = public_path().'/images/'.$file;
\File::delete($filename);
}
使用delete()
方法:
$image = DB::table('user_images')->where('id', $id)->first();
$file= $image->name;
$filename = public_path().'/images/'.$file;
\File::delete($filename);
DB::table('user_images')->where('id', $id)->delete();
UserImage::destroy($id);
public function destroy($id)
{
$image = DB::table('user_images')->where('id', $id);
if($image){
$file= $image->first()->name;
$filename = public_path().'/images/'.$file;
\File::delete($filename);
$image->delete();
}
//Silent fail. Silence is golden.
}
我正在尝试从 public 目录和数据库中删除上传的图片。我该怎么做?
我只能从 public 目录中删除,但我不知道如何从数据库中删除。谁能告诉我该怎么办 非常感谢
这是我用来从 public 目录中删除图片的控制器:
public function destroy($id)
{
$image = DB::table('user_images')->where('id', $id)->first();
$file= $image->name;
$filename = public_path().'/images/'.$file;
\File::delete($filename);
}
使用delete()
方法:
$image = DB::table('user_images')->where('id', $id)->first();
$file= $image->name;
$filename = public_path().'/images/'.$file;
\File::delete($filename);
DB::table('user_images')->where('id', $id)->delete();
UserImage::destroy($id);
public function destroy($id)
{
$image = DB::table('user_images')->where('id', $id);
if($image){
$file= $image->first()->name;
$filename = public_path().'/images/'.$file;
\File::delete($filename);
$image->delete();
}
//Silent fail. Silence is golden.
}