How to access the permission state of a file in Laravel

Overview

While working with files, you will notice that you get an error when you try to access some of them. This can be because the permission or visibility state of the application is set to private. In this shot, we will learn how to use the getVisibility() method to access the visibility or permission status of a file.

What is the getVisibility() method?

Laravel uses the getVisibility() method to check if a file is in its private or public state.

Syntax

Storage::getVisibility('file.jpg');

Parameters

The getVisibility() method receives the file name as a parameter.

Note: To use the getVisibility() method, we need to import a specific class with the use Illuminate\Support\Facades\Storage; method.

Example

$visibility = Storage::getVisibility('file.jpg');

This single line of code in our controller will carry out the functionality.

First, we use the Storage facade, which enables the application to interact with the files, and then we call the getVisibility() method on it. Inside the method, we pass the file.jpg parameter, which is a file whose visibility state we need to find.

Note: We will need to pass the path to the file so that the getVisibility() method can access it.

Free Resources