17

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 2

16

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.

1
  • 3
    thanks.you mean there is no need to File facade at all?Storage will do all jobs ? in documentation examples taylor has used file_get_contents() although he could use File::get() instead.maybe File is going to be deprecated.
    – alex
    Commented Mar 7, 2016 at 5:35
1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.