Lesson 10 – Laravel File System

The Laravel has its own file system.  You can find the configuration file for the file system in the config/filesystems.php file. You can configure all your “disks” or files within this file. Each of these disks represents a particular storage location and storage driver. The configuration file also contains details about how to configure each supported driver. You can simply modify these examples according to your own credentials and preferences.

It is also necessary to install the correct package through Composer before using any of the Rackspace or S3 drivers.

READ ALSO
Lesson 3 – Laravel Views

Here are the commands of both of these drivers.

• Amazon S3: league/flysystem-aws-s3-v2 ~1.0

• Rackspace: league/flysystem-rackspace ~1.0

It is worth mentioning that a single driver can support multiple disks. Similarly, you can modify as many disks as you like. Also, remember that the root directory controls all the file operations when you use the local driver. The root directory is defined in your configuration file and it is set to storage/app directory by default. For example, you can write the following piece of code to store a file in this directory.

Storage::disk('local')->put('file.txt', 'Contents');

You can use the storage utility to interact with and modify all of your storage disks. In the following lines, we share how you can perform different tasks related to storage disks using the storage façade.

READ ALSO
Lesson 9 – Events and Listeners

For retrieving a particular disk, you will use the following method.

$disk = Storage::disk('s3');
$disk = Storage::disk('local');

Similarly, the following method helps you determine whether or not a file exists.

$exists = Storage::disk('s3')->exists('file.jpg');

You can also call methods on the default disk by writing:

if (Storage::exists('file.jpg'))
{
https://dronaschool.com/ //95
}

For setting a file’s content, you need to write this method below.

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

The pieces of code below will append and prepend anything you want to a file.

Storage::append('file.log', 'Appended Text');
Storage::prepend('file.log', 'Prepended Text');   

Similarly, this is how you copy a certain file to a new folder.

Storage::copy('old/file1.jpg', 'new/file1.jpg');

With this, we come to the end of this basic Laravel course. As with all the courses, I highly recommend continuing your study and learn the more advanced concepts of this extremely powerful framework. It will help you speed up your development speed in addition to offering new career prospects to you.

READ ALSO
Lesson 2 – Artisan Commands