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?
2 Answers
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.
-
3thanks.you mean there is no need to
File
facade at all?Storage
will do all jobs ? in documentation examples taylor has usedfile_get_contents()
although he could useFile::get()
instead.maybeFile
is going to be deprecated.– alexCommented Mar 7, 2016 at 5:35
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.