在Laravel 5.4中存储和检索存储的文件 [英] storing and retrieving stored file in Laravel 5.4

查看:68
本文介绍了在Laravel 5.4中存储和检索存储的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的场景:

1)使用以下链接将公共/存储符号链接到存储/应用/公共:

1) Symlink public/storage to storage/app/public using

php artisan storage:link

2)使用

$path = $request->file('avatar')->store('avatars');

3)据我了解,我应该可以访问该网址下的文件-`www.example.com/storage/avatars/avatar.jpg

3) In my understanding, I should be able to access the file under this url - `www.example.com/storage/avatars/avatar.jpg

但是,我在第三步中获得的路径实际上是public/avatars/avatar.jpg,因此我最终将" public/"部分替换为",以获取正确的路径.

However, the path I get in the third step is actually public/avatars/avatar.jpg, so I end up replacing the 'public/' part with '' to have a correct path.

似乎我缺少了一些东西,但是我找不到确切的东西.

It seems that I am missing something, but I just can't find exactly what.

谢谢您的帮助!

推荐答案

使用公用磁盘时,应将文件存储在公用磁盘中.

When using the public disk, you should store the file in the public disk.

您可以在存储功能的第二个参数中指定磁盘.

You can specify the disk in the 2nd parameter of the store function.

$path = $request->file('avatar')->store(
    'avatars', 'public'
);

这可以确保文件存储在storage/app/public中,公共目录中的storage符号链接指向storage/app/public.

This makes sure that the file is stored in storage/app/public, the storage symlink in the public directory points to storage/app/public.

现在您可以简单地使用:

Now you can simply use:

asset('storage/avatars/avatar.jpg');

它将从storage/app/public/avatars/avatar.jpg

您可能需要检查storeAs重命名文件名或使用子目录来确保您的文件名是唯一的.

You might want to check storeAs to rename the filename or use a subdirectory to make sure your filenames are unique.

这篇关于在Laravel 5.4中存储和检索存储的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆