简体   繁体   English

Laravel 5.3文件系统存储/ putFile不起作用

[英]Laravel 5.3 Filesystem store/putFile not working

I am having a problem using the filesystem in Laravel 5.3. 我在Laravel 5.3中使用文件系统时遇到问题。 I am trying to save a file to my local storage but when I call either $file->store('directory', 'local') or Storage::putFile('directory', $file) it stores in the correct location storage/app/directory/filename but the path returned by both functions does not include the path to the storage/app directory, just directory/filename . 我正在尝试将文件保存到本地存储中,但是当我调用$file->store('directory', 'local')Storage::putFile('directory', $file)它将存储在正确的位置storage/app/directory/filename但是两个函数返回的路径都不包括到storage/app目录的路径,仅包括directory/filename

My local storage driver config is as below: 我的本地存储驱动程序配置如下:

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

Obviously I could wrap the $path with the storage_path() helper but this doesn't seem right when reading the docs. 显然,我可以使用storage_path()辅助函数包装$ path,但是在阅读文档时这似乎不正确。 https://laravel.com/docs/5.3/filesystem#storing-files https://laravel.com/docs/5.3/filesystem#storing-files

Am I doing wrong or missing something? 我做错了什么还是错过了什么?

it's not actually crazy. 这实际上并不疯狂。 The storage path is configurable, therefore if Laravel were to store it in database but you changed it down the line, there would be an issue. 存储路径是可配置的,因此,如果Laravel将其存储在数据库中,但是您将其更改为一行,则会出现问题。

What you can do simply is add storage_path($file) when you try to display your file in your blade template. 当您尝试在刀片模板中显示文件时,只需执行添加storage_path($file)即可。 That will automatically apply the proper path to storage before the file name and should find your file where it is. 这将自动在文件名之前将正确的路径应用于存储,并应在文件所在位置找到文件。

Tell me if that helped/worked 告诉我是否有帮助/有效

This is by design because in your config, you've already set the storage/app directory as your root directory. 这是设计使然,因为在配置中,您已经将storage / app目录设置为root目录。

So, it returns the path relative to your root. 因此,它返回相对于您的根的路径。

Take the case that you want it to return absolute rather than relative paths. 以您希望它返回绝对路径而不是相对路径的情况为例。 Now imagine if your Storage driver was an AWS S3 bucket. 现在,假设您的存储驱动程序是否是一个AWS S3存储桶。 In this case there is no absolute path. 在这种情况下,没有absolute路径。 The absolute path is a term specific to the local driver but the code was written to be extended/used by multiple drivers (and hence they wanted a consistent return value for the putfile method). 绝对路径是特定于本地驱动程序的术语,但是代码被编写为可被多个驱动程序扩展/使用(因此,他们需要putfile方法的一致返回值)。 So, relative path was the only choice in this case 因此,在这种情况下,相对路径是唯一的选择

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

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