How to Check If a File Exists in Laravel 10 Tutorial

Reading Time: 4 minutes
684 Views

Verifying the presence of a file is a typical need for file management, validation, and data retrieval operations in Laravel 10. Laravel provides handy methods to check if a file exists within your application, whether you need to ensure the existence of a specific file before executing activities or validate user-provided file paths.

In this detailed tutorial, we will be taking you through the process of testing if a file exists in Laravel 10. We will look at various strategies and best practises for validating the existence of files, regardless of where they are in your application’s file system.

Read More: Laravel 10 How To Change Name And From Address in Email

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.

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

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.

Read More: Laravel 10 How To Add Image Into PDF Using DomPDF Library

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.

Read More: Laravel 10 How To Work with Model Events And Listeners

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 10 Tutorial in a very detailed way.

Read More: Laravel 10 Export MySQL Table Data into CSV File Tutorial

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.