laravel 获取存储中文件的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43969105/
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 15:57:45  来源:igfitidea点击:

Get path to file in storage

laravellaravel-5laravel-5.3

提问by user651896

I saved a file to storage using:

我使用以下方法将文件保存到存储中:

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

Which saved it to:

将它保存到:

storage/app/avatars/avatar.png

How can I get the path to this file/folder (notthe URL)? What is the correct way to do this using Laravel's Filesystem?

如何获取此文件/文件夹的路径(不是URL)?使用Laravel 的文件系统执行此操作的正确方法是什么?

回答by ErcanE

Try this

尝试这个

storage_path('app/avatars/avatar.png');

storage_path('app/avatars/avatar.png');

回答by sisve

There is no correct way to do this; because it should not be done. The Storage is an opaque system to talk to different storage systems; as such there is no api to get the backing file path. As an example, that wouldn't work with Amazon S3. The only path your application knows about is the string you send to the Storage facade to work with the file, there are no guarantees that this string is used to generate the filename when the storage system stores the file.

没有正确的方法可以做到这一点;因为不应该这样做。Storage 是一个不透明的系统,可以与不同的存储系统进行通信;因此没有 api 来获取后备文件路径。例如,这不适用于 Amazon S3。您的应用程序知道的唯一路径是您发送到 Storage 外观以处理文件的字符串,无法保证在存储系统存储文件时使用此字符串生成文件名。

There are some hacks you can use that works for the local disk, but those are not available for the Storage system in general. Using these solutions means that you'll limit yourself to only use the local disk; this will cause you troubles when you need to scale out and add another server. You'll then have two servers with two separate local disks, with separate content.

您可以使用一些适用于本地磁盘的技巧,但这些技巧通常不适用于存储系统。使用这些解决方案意味着您将限制自己只能使用本地磁盘;当您需要向外扩展并添加另一台服务器时,这会给您带来麻烦。然后,您将拥有两台服务器和两个单独的本地磁盘,以及单独的内容。

The correct way to work with the files, that will work for all configurations, is to get the file content (Storage::get), do the modifications (including storing them in a temporary file) and then write back the new file content (Storage::set).

处理文件的正确方法(适用于所有配置)是获取文件内容 ( Storage::get),进行修改(包括将它们存储在临时文件中),然后写回新的文件内容 ( Storage::set)。

If you're really sure that you will only ever use the local filesystem, use the File facadeinstead of the Storage facade. I'm unable to find any documentation for this, only the interface it exposes.

如果您真的确定只会使用本地文件系统,请使用File 门面而不是 Storage 门面。我找不到任何相关文档,只能找到它公开的界面。

Reference: https://github.com/laravel/framework/issues/13610

参考:https: //github.com/laravel/framework/issues/13610

回答by Vivek Chaudhari

you can only get the storage folder path from laravel function, you can give nested folder name after it, it will bind the base url as well

你只能从 Laravel 函数中获取存储文件夹路径,你可以在它后面给出嵌套文件夹名称,它也会绑定基本 url

storage_path(folder1/folder2/.../file.png);