Laravel 显示来自 storage/app/public 的图像

Laravel display images from storage/app/public

我正在尝试显示 storage/app/public 文件夹中的图像。每当我的控制器使用

从该文件夹中检索图像时
$images = Storage::files('public');

从此处检索到的文件带有 public/ 前缀,而不是 storage/。 这导致的问题是可以找到图像 localhost/storage/filename.jpg,但找不到 localhost/public/filename.jpglocalhost/storage/public/filename.jpg。 我可以通过手动切断 public 前缀并将其替换为存储来解决此问题,但这并不是一种特别优雅的处理方式。

我有 运行 命令 php artisan storage:link 并重新运行它会产生一条 The "public/storage" directory already exists. 消息。

Laravel 在存储配置中带有内置 public 磁盘。

要列出 storage/app/public 文件夹 中没有 前缀的所有文件,请使用:

\Storage::disk('public')->files();

要在 storage/app/public 文件夹中获取正确的 URL 文件,请使用:

\Storage::disk('public')->url($file);

到returnstorage/app/public文件夹内的URL个文件数组,这里是一个示例组合:

array_map(function ($f) {
    return \Storage::disk('public')->url($f);
}, \Storage::disk('public')->files());