Laravel File vs Storage facade

余生长醉 提交于 2021-01-19 14:29:49

问题


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?


回答1:


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

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.




回答2:


The File Facade just 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');

The Storage Facade contains 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');

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

Also you can save raw file contents into a file like this:

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



来源:https://stackoverflow.com/questions/35828702/laravel-file-vs-storage-facade

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