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

42 Views

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

PHP | file_exists( ) Function

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

 file_exist("abc.jpg")
 

The file_exists() function is an inbuilt function in PHP which 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";
}
?>

 

Tags:-
PHP