How to Check If File Exists or Not in Laravel

AuthorSumit Dey Sarkar

Pubish Date28 Jan 2024

categoryLaravel

In this tutorial, we will learn how to check If file exists or not in Laravel.

How to Check If File Exists or Not in Laravel?

Determining If a File Exists in Laravel

In Laravel, you can use the File facade to check if a file exists.

Let's see an example of how you can check if a file exists in Laravel or not:

use Illuminate\Support\Facades\File;

$filePath = 'path/to/your/file.txt';

if (File::exists($filePath)) {
    echo "The file exists.";
} else {
    echo "The file does not exist.";
}

Replace path/to/your/file.txt with the actual path to the file which you want to check.

Alternatively, you can use the file_exists function, which is a PHP core function, but it works the same way:

$filePath = 'path/to/your/file.txt';

if (file_exists($filePath)) {
    echo "The file exists.";
} else {
    echo "The file does not exist.";
}

Both methods will return true if the file exists and false if it does not. Choose the method that fits your preferences or the conventions of your project.

Comments 0

Leave a comment