获取存储中文件的路径 [英] Get path to file in storage

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

问题描述

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

I saved a file to storage using:

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

将其保存到:

storage/app/avatars/avatar.png

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

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

推荐答案

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

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.

您可以使用一些适用于本地磁盘的技巧,但是这些技巧通常不适用于存储系统.使用这些解决方案意味着您将只能使用本地磁盘.当您需要横向扩展并添加另一台服务器时,这将给您带来麻烦.然后,您将拥有两台服务器,其中包含两个单独的本地磁盘,每个内容具有单独的内容.

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.

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

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).

如果您确实确定只使用本地文件系统,请使用

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

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

这篇关于获取存储中文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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