Laravel:如何获取存储在 storage/app/ 中的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47877203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:07:47  来源:igfitidea点击:

Laravel : How to get a file stored in storage/app/?

phplaravellaravel-5

提问by Hiroki

I've been trying to store a file to (and get it from) storage/app/, following the doc.

我一直在尝试storage/app/按照doc将文件存储到(并从中获取)。

After I run php artisan storage:link, I can see that there is a link to the puiblic folder.

运行后php artisan storage:link,我可以看到有一个指向 puiblic 文件夹的链接。

If I put a file with the following code...

如果我放了一个带有以下代码的文件...

    Storage::put('/images/blah.png', $file);

...the storage will look like this.

...存储看起来像这样。

I'm not really sure if the link is working properly, since this blah.pngcan't be retrieved.

我不确定链接是否正常工作,因为blah.png无法检索。

So far, I've changed the access level of storageby chmod -R 755 storage/, and put the complete URL (http://localhost:8000/storage/images/blah.png. This is what asset('storage/images/blah.png')returns). Still, I'm getting 404 error (Not Found).

到目前为止,我已经更改了storageby的访问级别chmod -R 755 storage/,并放置了完整的 URL ( http://localhost:8000/storage/images/blah.png。这是asset('storage/images/blah.png')返回的内容)。尽管如此,我还是收到了 404 错误(未找到)。

In config/filessystems.php. the default is set to be local. (i.e. 'default' => 'local')

config/filessystems.php. 默认设置为local. (即'default' => 'local'

Do you see anything I'm doing wrong?

你看到我做错了什么吗?

Any advice will be appreciated.

任何建议将被认真考虑。

PS

聚苯乙烯

This is how the configuration of the public disk looks like...

公盘的配置是这样的……

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

回答by Ben

The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public.

公共磁盘用于将要公开访问的文件。默认情况下,公共磁盘使用本地驱动程序,并将这些文件存储在 storage/app/public 中。为了使它们可以从网络访问,您应该创建一个从 public/storage 到 storage/app/public 的符号链接。

To create a symbolic link from public/storage to storage/app/public, go to the root folder:

要创建从 public/storage 到 storage/app/public 的符号链接,请转到根文件夹:

ln -s storage/app/public public/storage

Then, you can save the file to storage/app/public:

然后,您可以将文件保存到 storage/app/public:

Storage::put('/public/images/blah.png', $file);
// the blah.png is located at storage/app/public/images/blah.png
// you can also get the file path by
// $image_path = storage_path() . "/app/public/images/blah.png";

Then, you can access the image file as:

然后,您可以通过以下方式访问图像文件:

http://{domain}/storage/images/blah.png

回答by nelson manuel lugo cahuana

in the controller

在控制器中

Storage::disk('images')->get('firmas.jpg');

and in the file config/filesystems.php

并在文件 config/filesystems.php 中

'default' => env('FILESYSTEM_DRIVER', 'local'),

and the disk

和磁盘

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'images' => [ // used for Backpack/CRUD (in elFinder)
        'driver' => 'local',
        'root'   => storage_path('app/pdfs'),
    ],

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

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

]