Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Check If File Exists Using Laravel

In this Article, we will discuss 3 ways to check if a file Exists using Laravel’s functionalities.

Storage Facade

Let’s start off with the Storage Facade which is part of the Laravel filesystem.

It provides an easy way to check if a file exists within the project’s storage directory.

We can use the exists('path_to_file') method which takes a single argument, which is the path to the file, and returns true if the file exists, or false otherwise.

File Facade

Another way is to use the File facade which is also part of the Laravel filesystem.

We can use the same exists('path_to_file') method or the isFile('path_to_file') method.

Now let’s get to the cool part!


Storage vs File Facade

Which one to use?

Under the hood, both exists() and isFile() methods use the PHP built-in functions ( file_exists() & is_file() ) without any additional logic.

You can check the source code here.

It’s up to you to decide which way you wanna go.

But to have a good developer experience while working on your project, it’s good to have in mind the differences between the File and Storage Facades.

The File facade is used to interact with the local file system, while the Storage facade is often used to interact with cloud-based storage operations such as Amazon S3, Google Cloud Storage. and more.

It’s better to use the appropriate Facade because that hints you the environment you’re working with.

Happy coding


Let me know what you think about this article through comments below, or on Twitter at @pelu_carol.

If you find this article helpful, please share it with others and subscribe to the blog to support me, and receive a bi-monthly-ish e-mail notification on my latest articles.



This post first appeared on Neutron Dev, please read the originial post: here

Share the post

How to Check If File Exists Using Laravel

×

Subscribe to Neutron Dev

Get updates delivered right to your inbox!

Thank you for your subscription

×