如何从 Laravel 存储子文件夹中检索文件?

发布于 2025-01-14 04:30:56 字数 1073 浏览 2 评论 0原文

我正在尝试从 Laravel 项目中的此路径检索文件:

/storage/app/public/blog/3.jpg

这些方法会产生以下错误:

1.

$image = Storage::disk('public')->get('/storage/blog/3.jpg');

->

local.ERROR: File not found at path: storage/blog/3.jpg {"userId":16,"exception":"[object] (Illuminate\\Contracts\\Filesystem\\FileNotFoundException(code: 0): File not found at path: storage/blog/3.jpg at /Users/artur/PhpstormProjects/safa-ameedee.com/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:171)
[stacktrace]
$image = Storage::disk('public')->get('/storage/app/public/blog/3.jpg');
$image = Storage::get('/storage/app/public/blog/3.jpg');

->

local.ERROR: File not found at path: storage/app/public/blog/3.jpg {"userId":16,"exception":"[object]

奇怪的是,我将文件存储在存储中,如下所示:

Storage::disk('public')->put('/blog/' . $request->path, $image);

那么它们不应该以相同的方式检索吗?

I'm trying to retrieve a file from this path in a laravel project:

/storage/app/public/blog/3.jpg

These approaches produce following errors:

1.

$image = Storage::disk('public')->get('/storage/blog/3.jpg');

->

local.ERROR: File not found at path: storage/blog/3.jpg {"userId":16,"exception":"[object] (Illuminate\\Contracts\\Filesystem\\FileNotFoundException(code: 0): File not found at path: storage/blog/3.jpg at /Users/artur/PhpstormProjects/safa-ameedee.com/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:171)
[stacktrace]
$image = Storage::disk('public')->get('/storage/app/public/blog/3.jpg');
$image = Storage::get('/storage/app/public/blog/3.jpg');

->

local.ERROR: File not found at path: storage/app/public/blog/3.jpg {"userId":16,"exception":"[object]

The weird thing is that I store the files in the storage like so:

Storage::disk('public')->put('/blog/' . $request->path, $image);

So should they not be retrievable in the same way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋叶绚丽 2025-01-21 04:30:56

TL/DR

Storage::disk('public')->get('block/3.jpg');

解释

问题是您出于某种原因将存储放入路径中。这是没有必要的,而且会导致建立错误的道路。

看一下默认的文件系统配置:

'disks' => [
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
        'throw' => false,
    ],
    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
        'throw' => false,
    ],

root 是在这里看到的有用的内容。 storage_path() 返回存储文件夹的完整路径。所以类似于 storage_path('folder_1') -> /home/user/project/storage/folder_1

本地磁盘是默认的,因此只需执行Storage::get()就会自动使用它。

您使用的是 public 磁盘,因此这些文件的实际位置是 storage/public(符号链接到 public/storage)。这意味着 Storage::disk('public') 已经从 /home/user/project/storage/app/public 开始。再次添加storage会使路径不正确。

使用 path 可能有助于将来的调试。 Storage::disk('public')->path('block/3.jpg') 会输出完整路径,你可以看到哪里出了问题。对于给定的代码,它可能会显示类似 /home/user/project/storage/app/public/storage/app/public 的内容。

TL/DR

Storage::disk('public')->get('block/3.jpg');

Explanation

The problem is you're putting storage in the path for some reason. It's not necessary and is leading to the wrong path being built.

Take a look at the default filesystems config:

'disks' => [
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
        'throw' => false,
    ],
    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
        'throw' => false,
    ],

The root is what is useful to see here. storage_path() returns the full path to the storage folder. So something like storage_path('folder_1') -> /home/user/project/storage/folder_1.

The local disk is the default, so just doing Storage::get() will use it automatically.

You're using the public disk, so the actual location of these files is storage/public (symlinked into public/storage). This means doing Storage::disk('public') already begins at /home/user/project/storage/app/public. Adding storage again makes the path incorrect.

Using path may help with future debugging. Storage::disk('public')->path('block/3.jpg') will output the full path, and you can see where it's going wrong. For your code given it would probably show something like /home/user/project/storage/app/public/storage/app/public.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文