Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel File vs Storage facade

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?

like image 473
alex Avatar asked Mar 06 '16 15:03

alex


People also ask

What is storage file in Laravel?

Laravel's filesystem configuration file is located at config/filesystems.php . Within this file, you may configure all of your filesystem "disks". Each disk represents a particular storage driver and storage location.

What is a Laravel facade?

In a Laravel application, a facade is a class that provides access to an object from the container. The machinery that makes this work is in the Facade class. Laravel's facades, and any custom facades you create, will extend the base Illuminate\Support\Facades\Facade class.

How can I get Laravel storage file?

How can I get Laravel storage file? Laravel's filesystem configuration file is located at config/filesystems. php . Within this file, you may configure all of your filesystem "disks".

What does Laravel use for seeding & facades?

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.


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.

like image 74
Christoffer Tyrefors Avatar answered Oct 27 '22 12:10

Christoffer Tyrefors


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.

like image 27
Adam Avatar answered Oct 27 '22 13:10

Adam