在Laravel中,如何获取公用文件夹中所有文件的列表? [英] In Laravel, how can I obtain a list of all files in a public folder?

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

问题描述

我想自动生成公用文件夹中所有图像的列表,但似乎找不到任何可以帮助我完成此操作的对象.

I'd like to automatically generate a list of all images in my public folder, but I cannot seem to find any object that could help me do this.

Storage类似乎很适合这份工作,但是它只允许我在公共文件夹之外的存储文件夹中搜索文件.

The Storage class seems like a good candidate for the job, but it only allows me to search files within the storage folder, which is outside the public folder.

推荐答案

您可以为Storage类创建另一个磁盘.我认为这将是您的最佳解决方案.

You could create another disk for Storage class. This would be the best solution for you in my opinion.

在磁盘阵列的 config/filesystems.php 中,添加所需的文件夹.在这种情况下为 public 文件夹.

In config/filesystems.php in the disks array add your desired folder. The public folder in this case.

    'disks' => [

    'local' => [
        'driver' => 'local',
        'root'   => storage_path().'/app',
    ],

    'public' => [
        'driver' => 'local',
        'root'   => public_path(),
    ],

    's3' => '....'

然后,您可以通过以下方式使用 Storage 类在公用文件夹中工作:

Then you can use Storage class to work within your public folder in the following way:

$exists = Storage::disk('public')->exists('file.jpg');

$ exists变量将告诉您 public 文件夹中是否存在 file.jpg ,因为存储磁盘"public" 指向public项目文件夹.

The $exists variable will tell you if file.jpg exists inside the public folder because the Storage disk 'public' points to public folder of project.

您可以将文档中的所有 Session 方法与自定义磁盘一起使用.只需添加disk('public')部分.

You can use all the Session methods from documentation, with your custom disk. Just add the disk('public') part.

 Storage::disk('public')-> // any method you want from 

http://laravel.com/docs/5.0/filesystem#basic-usage

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

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