File Laravel vs Facade Storage

Are there any differences between File and Storage facades in laravel 5.2?
it seems that they both use the same contract. There is no documentation for File laravel documentation. if they are different, how can they interact with each other?

+13
file facade laravel storage
source share
2 answers

A file is a fairly simple wrapper for PHP functions like file_exists (), etc. Storage is "a powerful file system abstraction thanks to Frank de Jonge's excellent Flysystem PHP package." This can be used for local files (i.e. Storage::disk('local')->exists('path') ).

Prior to Laravel 5, Laravel did not have Flysystem integration. At that time, the facade of the file was a β€œway” to interact (local files). I would suggest that the documentation for the File has been removed so that users use storage instead. The file system really works.

+11
source share

The file facade simply contains some primitive methods that work only with an 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');

Storage Facade contains a set of complex methods and is a wrapper for other third-party tools.

The first advantage is that you can use the relative folder path:

  • Storage::makeDirectory('uploads/14214');
  • Storage::copy('uploads/14214/test.json', 'uploads/99999/test.json');

You can change the default folder /storage/app in config/filesystems.php or create other disks that you can call using Storage::disk('specialxyz')->copy(...) .

You can also save raw file contents to a file as follows:

  • Storage::put('file.jpg', $contents);

And my favorite, it is very easy to upload custom files using

 $path = Storage::putFile('avatars', $request->file('avatar')); 

or

  $path = $request->file('avatar')->store('avatars'); 

By default, the store method generates a unique identifier as a file name. The file extension will be determined by examining the MIME file type. The file path will be returned by the store method so that you can save the path, including the generated file name, in your database.

0
source share

All Articles