简体   繁体   English

Laravel:存储未将文件放入公用文件夹

[英]Laravel: Storage not putting file inside public folder

When I use:当我使用:

Storage::putFile('documents', $content, 'public');

I expect the folder structure inside public directory, like:我希望公共目录中的文件夹结构,例如:

+
+-- storage
+--|-- app
+--|--|-- public
+--|--|--|-- documents
+--|--|--|--|-- XyZ.pdf
+

Instead of:代替:

+
+-- storage
+--|-- app
+--|--|-- documents
+--|--|--|-- XyZ.pdf
+--|--|-- public
+

So, to force storage at "right" place I need to explicit put:因此,要强制存储在“正确”的位置,我需要明确放置:

Storage::putFile('public/documents', $content, 'public');

What returns me a path that I'm not able to save at database because its values is "public/documents/xyz.pdf" and will not work properly with asset() helper.什么返回了我无法在数据库中保存的路径,因为它的值是"public/documents/xyz.pdf"并且不能与asset() helper 一起正常工作。

How to use Storage facade to put files inside storage/app/public ?如何使用Storage门面将文件放入storage/app/public

This isn't explicity in the docs.这在文档中并不明确。

Looking at config/filesystems.php , we have a public disk.查看config/filesystems.php ,我们有一个public磁盘。 Just use it:只需使用它:

Storage::disk('public')->put('documents', $content);

For anyone who wants every file to be stored in public and also wishes framework to magically handle the storage location is by using .env files.对于希望每个文件都公开存储并希望框架神奇地处理存储位置的任何人来说,都是使用.env文件。 Open your .env file and you will find following line打开你的 .env 文件,你会发现下面一行

FILESYSTEM_DRIVER=local

Change the value to public将值更改为public

FILESYSTEM_DRIVER=public

Now all your storage method will by default store all files inside storage/app/public directory.现在,您的所有存储方法默认将所有文件存储在storage/app/public目录中。 Regardless of methods you use无论您使用哪种方法

Storage::store
Storage::storeAs
Storage::put
Storage::putFileAs
$request->file('avatar')->storeAs

Note: If there's anything you want to store locally (inside non-public directory), then you have to mention the disk as注意:如果您想在本地存储任何内容(在非公共目录中),那么您必须将磁盘提及为

Storage::disk('local')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM