如何从 Laravel 存储子文件夹中检索文件?
我正在尝试从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TL/DR
解释
问题是您出于某种原因将
存储
放入路径中。这是没有必要的,而且会导致建立错误的道路。看一下默认的文件系统配置:
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
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:
The
root
is what is useful to see here.storage_path()
returns the full path to the storage folder. So something likestorage_path('folder_1')
->/home/user/project/storage/folder_1
.The
local
disk is the default, so just doingStorage::get()
will use it automatically.You're using the
public
disk, so the actual location of these files isstorage/public
(symlinked intopublic/storage
). This means doingStorage::disk('public')
already begins at/home/user/project/storage/app/public
. Addingstorage
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
.