简体   繁体   English

Laravel 6 Blade - 显示公共文件夹中的所有图像?

[英]Laravel 6 Blade - Display all images inside a public folder?

I am having trouble figuring out a way to reach and display the contents of a folder located in 'public/images'.我无法找到一种方法来访问和显示位于“public/images”中的文件夹的内容。 The goal is to have a blade file that acts as a gallery and displays all the images added to that specific folder.目标是拥有一个刀片文件,用作画廊并显示添加到该特定文件夹的所有图像。

I know how to access specific files using asset('images/imagename.png') but I need to get all of them at once.我知道如何使用asset('images/imagename.png')访问特定文件,但我需要一次获取所有文件。

Can I simply create a loop inside of the blade file or do I need to make a controller/route for it?我可以简单地在刀片文件中创建一个循环,还是需要为它制作一个控制器/路由?

Try this:尝试这个:

@foreach(File::glob(public_path('images').'/*') as $path)
   <img src="{{ str_replace(public_path(), '', $path) }}">
@endforeach

You could do use glob method of the File facade if you know the directory.如果您知道目录,您可以使用File门面的glob方法。 The following code assumes every file in the public/images/ folder is an image.以下代码假定public/images/文件夹中的每个文件都是一个图像。

<div class="container">
    @forelse (File::glob('public/images/*') as $file)
        <img src="{{ $file }}">
    @else
        <p id="no-images">No images in folder</p>
    @endforelse
</div>

As the documentation implies, you can get all files in a specific directory with:正如文档所暗示的那样,您可以使用以下命令获取特定目录中的所有文件:

Storage::files($directory);

So, you can iterate like this:所以,你可以像这样迭代:

@foreach(Storage::files($directory) as $file)
    <img src="{{ $file }}">
@endforeach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM