Laravel 文件与存储外观
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35828702/
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
Laravel File vs Storage facade
提问by amin
Is there any differences between File
and Storage
facades in laravel 5.2 ?
it seems they both use the same contract.i see no documentation for File
in laravel documentation.
if they are different how may interact with each other?
laravel 5.2File
和Storage
facades之间有什么区别吗?
似乎他们都使用相同的合同。我File
在 laravel 文档中没有看到任何文档。如果它们不同,如何相互交互?
回答by Christoffer Tyrefors
File is a quite simple wrapper for PHP functions such as file_exists() etc. Storage is "a powerful filesystem abstraction thanks to the wonderful Flysystem PHP package by Frank de Jonge". This can be used to act on local files (i.e Storage::disk('local')->exists('path')
).
File 是一个非常简单的 PHP 函数包装器,例如 file_exists() 等。Storage 是“一个强大的文件系统抽象,感谢 Frank de Jonge 出色的 Flysystem PHP 包”。这可用于处理本地文件(即Storage::disk('local')->exists('path')
)。
Prior to Laravel 5, Laravel had no Flysystem integration. At that time, the File facade was "the way" to interact with (local files). I would guess that the documentation for File is removed in order to make users use the Storage instead. The Filesystem does work though.
在 Laravel 5 之前,Laravel 没有 Flysystem 集成。当时,文件外观是与(本地文件)交互的“方式”。我猜想删除文件的文档是为了让用户改用存储。不过文件系统确实有效。
回答by Adam
The File Facadejust contains some primitive methods that work only with absolute path or relative to your script:
该文件门面只是包含了一些原始的方法,只有以绝对路径或相对于你的脚本工作:
\File::makeDirectory('/home/www/myProject/storage/app/uploads/14214');
\File::copy('/home/www/myProject/storage/app/uploads/14214/test.json', '/home/www/myProject/storage/app/uploads/99999/test.json');
\File::makeDirectory('/home/www/myProject/storage/app/uploads/14214');
\File::copy('/home/www/myProject/storage/app/uploads/14214/test.json', '/home/www/myProject/storage/app/uploads/99999/test.json');
The Storage Facadecontains a set of complex methods and is a wrapper for other 3rd party tools.
该 存储门面包含了一套复杂的方法,是其他第三方工具的包装。
The first advantage is, you can use relative path to a folder:
第一个优点是,您可以使用文件夹的相对路径:
Storage::makeDirectory('uploads/14214');
Storage::copy('uploads/14214/test.json', 'uploads/99999/test.json');
Storage::makeDirectory('uploads/14214');
Storage::copy('uploads/14214/test.json', 'uploads/99999/test.json');
you may change the default folder /storage/app
in config/filesystems.php
or create other disks that you may call with Storage::disk('specialxyz')->copy(...)
.
您可以更改默认的文件夹/storage/app
中config/filesystems.php
,或者创建其他磁盘,您可以与调用Storage::disk('specialxyz')->copy(...)
。
Also you can save raw file contents
into a file like this:
您也可以保存raw file contents
到这样的文件中:
Storage::put('file.jpg', $contents);
Storage::put('file.jpg', $contents);
And my favorite, its very easy to upload user files by
我最喜欢的是,通过以下方式上传用户文件非常容易
$path = Storage::putFile('avatars', $request->file('avatar'));
or
或者
$path = $request->file('avatar')->store('avatars');
By default, the store method will generate a unique ID to serve as the file name. The file's extension will be determined by examining the file's MIME type. The path to the file will be returned by the store method so you can store the path, including the generated file name, in your database.
默认情况下,store 方法会生成一个唯一的 ID 作为文件名。文件的扩展名将通过检查文件的 MIME 类型来确定。文件的路径将由 store 方法返回,因此您可以在数据库中存储路径,包括生成的文件名。