How will you check if a file exists or not using PHP?

Last updated 5 years ago | 2200 views 75     5

Tags:- PHP

PHP | file_exists( ) Function

The file's existence can be confirmed using the file_exist() function which takes the file name as an argument.

file_exist("abc.jpg")
 

The file_exists() function is an inbuilt function in PHP that is used to check whether or not a file or directory exists.

The path of the file or directory you want to check is passed as a parameter to the file_exists() function which returns true on success and false on failure.

Syntax:

file_exists($filename)

filename: Path to the file or directory.

Return Value:

Returns TRUE if the file or directory specified by "filename" exists; FALSE otherwise.

Let's see an example of file_exist() function.

<?php
// select file name with path
$filename = '/path/to/zone.txt';

// condition to check file exist or not
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?>