【问题标题】:storing and retrieving stored file in Laravel 5.4在 Laravel 5.4 中存储和检索存储的文件
【发布时间】:2017-05-18 18:50:28
【问题描述】:

一个简单的场景:

1) 将 public/storage 符号链接到 storage/app/public 使用

php artisan storage:link

2) 使用上传文件

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

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

但是,我在第三步得到的路径实际上是 public/avatars/avatar.jpg,所以我最终将 'public/' 部分替换为 '' 以获得正确的路径。

我似乎遗漏了一些东西,但我就是找不到确切的东西。

感谢您的帮助!

【问题讨论】:

    标签: php laravel-5


    【解决方案1】:

    使用公盘时,应将文件存放在公盘中。

    可以在store函数的第二个参数中指定磁盘。

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

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

    现在您可以简单地使用:

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

    它会从storage/app/public/avatars/avatar.jpg检索文件


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

    【讨论】:

    • 老兄,这太完美了:)
    猜你喜欢
    • 2018-08-24
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多