How to Check File is Exists or Not in PHP?

By Hardik Savani November 5, 2023 Category : PHP

Hi Developer,

In this quick guide, we will teach you how to check if a file exists in PHP. We will provide an example of checking if a file with a URL image exists using PHP. You will learn how to check if an image exists in PHP. Please follow the tutorial steps below to learn how to check if a file exists in PHP.

To check whether a file exists in PHP, you can use the file_exists() function. This function takes a file path as its parameter and returns a boolean value of true if the file exists and false otherwise. Here's an example:

index.php

<?php

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

if (file_exists($filePath)) {

echo 'File exists';

} else {

echo 'File does not exist';

}

?>

In this example, the file_exists() function is used to check whether a file with the path path/to/file.txt exists or not. If the file exists, the message "File exists" is printed to the screen. Otherwise, the message "File does not exist" is printed.

I hope it can help you...

Tags :
Shares