How to Check If a File Exists in Laravel 8 Tutorial

Reading Time: 4 minutes
9,157 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 8 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.


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

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

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 8 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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more