在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