How to change file system in Laravel using the disk() method

widget

In Laravel, when it comes to uploading and downloading from your application, you need to talk about file systems.

What is a file system in Laravel?

A file system allows for communication or is a driver that allows your application work with your local files. In Laravel, this function is active due to the integration of the flysystem package.

Available filesystem driver

  1. Local
  2. Amazon S3
  3. Rackspace Cloud storage.

In this shot we will focus on how to change these drivers so that even though we have files on both the local and Amazon S3, we can retrieve them easily.

To do this, we will be using the disk() method.

The disk() method is used to change this driver, as we will see shortly from the syntax.

Syntax

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

As mentioned earlier, this method is used often if you are using multiple disks.

The above code shows how we are saving the disk variable as an Amazon S3 instance.

Parameter(s)

The disk() receives the name of the disk you want to use as the parameter.

By default, it is the local disk. Let’s say you want to change it to Amazon S3. The above syntax is the right way to do so, as you just need to pass the name of the driver.

Example

The example below shows how we use the disk() method to select a disk to check if the file.jpg file exists on the disk.

if (Storage::disk('s3')->exists('file.jpg')) {
    // ...
}

Output

There is nothing to actually visualize, as the whole action is a process.