如何从Laravel 5.1中的公用文件夹中删除文件 [英] How to delete file from public folder in laravel 5.1

查看:62
本文介绍了如何从Laravel 5.1中的公用文件夹中删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中删除新闻,当我点击删除按钮时,数据库中的所有数据都被删除,但图像仍保留在上载文件夹中. 因此,我该如何工作. 谢谢

I want to delete a News from database and when I hit the delete button all data from database deleted but the image is remains in upload folder. So, how do I this to work. thanks

这又是我的功能,但不会从公共目录的images/news文件夹中删除图像.

This is my function again but does not delete the image from images/news folder of public directory>

 public function destroy($id) {
    $news = News::findOrFail($id);
    $image_path = app_path("images/news/{$news->photo}");

    if (File::exists($image_path)) {
        //File::delete($image_path);
        unlink($image_path);
    }
    $news->delete();
    return redirect('admin/dashboard')->with('message','خبر موفقانه حذف  شد');
}

推荐答案

您可以按照@Khan的建议使用PHP的unlink()方法.

You could use PHP's unlink() method just as @Khan suggested.

但是,如果要使用Laravel方法,请改用File :: delete()方法.

But if you want to do it the Laravel way, use the File::delete() method instead.

//删除单个文件

File::delete($filename);

//删除多个文件

File::delete($file1, $file2, $file3);

//删除文件数组

$files = array($file1, $file2);
File::delete($files);

别忘了在顶部添加:

use Illuminate\Support\Facades\File; 

这篇关于如何从Laravel 5.1中的公用文件夹中删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆