Get path to file in storage

只谈情不闲聊 提交于 2019-12-12 01:48:48

问题


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 (not the URL)? What is the correct way to do this using Laravel's Filesystem?


回答1:


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.

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.

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




回答2:


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

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



回答3:


Try this

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



来源:https://stackoverflow.com/questions/43969105/get-path-to-file-in-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!