How to Check If a File Exists in Laravel 9 Tutorial

Reading Time: 4 minutes
6,297 Views

Inside this tutorial we will see the concept of checking file existence in /public folder or /storage folder. This tutorial will give you the classified information in a very clear way.

How to check if a file exists in laravel 9 application, we will discuss inside this article. Generally we store public accessible files in /public folder or even in /storage folder. So while accessing those files always need to add a checkpoint that they exists or not.

We can check all types of application files like pdf, image, doc etc.

In this file existence checking concept also we will use a php function file_exists() and File::exists() laravel facade class.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Checking File Existence inside Public folder

If we store any file like an image, pdf, etc inside /public folder, laravel application gives a helper function to access those.

Helper function as

public_path("FILE PATH")

This helper function looks specified file inside /public folder.

Check File Exists

Suppose we have stored an image 85214563.jpg inside images folder of /public.

Example #1

if(file_exists(public_path('images/85214563.jpg'))){

    dd('File exists.');
}else{
    dd('File not exists.');
}

Here, we used file_exists php function to check.

Example #2

if(File::exists(public_path('images/85214563.jpg'))){

    dd('File is exists.');
}else{
    dd('File is not exists.');
}

File::exists(), File if a facade and exists a method from it. Before use you need to add this to header

use Illuminate\Support\Facades\File;

Checking File Existence inside Storage folder

If we store any file like an image, pdf, etc inside /storage folder, laravel application gives a helper function to access those.

Helper function as

storage_path("FILE PATH")

This helper function looks specified file inside /storage folder.

Check File Exists

Suppose we have stored an image 85214563.jpg inside images folder of /storage.

Example #1

if(file_exists(storage_path('images/85214563.jpg'))){

    dd('File exists.');
}else{
    dd('File not exists.');
}

Here, we used file_exists php function to check.

Example #2

if(File::exists(storage_path('images/85214563.jpg'))){

    dd('File is exists.');
}else{
    dd('File is not exists.');
}

File::exists(), File if a facade class and exists a method from it. Before use you need to add this to header

use Illuminate\Support\Facades\File;

We hope this article helped you to learn How to Check If a File Exists in Laravel 9 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.