How to Check If File Exists or Not in Laravel?
Hey Dev,
In this article, we will cover how to check file is exist or not in laravel. you will learn check file exists or not in laravel. This article goes in detailed on laravel check if image exists in storage. I would like to show you laravel check if file exists in storage.
There are several ways to check file is exist or not in laravel. i will give you following three example using Storage, File and file_exists() to check file exists or not.
So, let's see the example code:
Example 1: using Storage
In Laravel, you can use the Storage facade to check if a file exists or not. The Storage facade provides a convenient way to work with local and cloud-based storage solutions. Here's an example:
Example Code:
use Illuminate\Support\Facades\Storage;
if (Storage::exists('images/file.jpg')) {
/* File exists */
} else {
/* File doesn't exist */
}
In the above example, the exists() method of the Storagev facade is used to check if the file file.jpg exists in the images directory. If the file exists, the code inside the if block will be executed, otherwise the code inside the else block will be executed.
Example 2: using File
You can also use the File facade to check if a file exists. Here's an example:
Example Code:
use Illuminate\Support\Facades\File;
if (File::exists(public_path('images/file.jpg'))) {
/* File exists */
} else {
/* File doesn't exist */
}
Example 3: using file_exists()
You can also use the file_exists php function to check if a file exists. Here's an example:
Example Code:
if(file_exists(public_path('images/1461177230.jpg'))){
dd('File is exists.');
}else{
dd('File is not exists.');
}
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Merge Multiple PDF Files in Laravel 10?
- Laravel Get Min Value of Column Example
- Laravel Check If Relationship Data is Empty Example
- How to Convert Number to Words in Laravel?
- FCM Push Notification in Laravel Example
- How to Check Laravel Version of a Your Project?
- How to Install Laravel in Ubuntu Server?
- Laravel Carbon Check Date is in Past Date Code
- Laravel Carbon Get Year from Date Example
- How to Check Input File is Empty or Not in Laravel?
- How to Check Request Method is GET or POST in Laravel?
- Laravel Blade Check if View Exists or Not Example
- How to Get Last Executed Query in Laravel?