Tuesday, July 7, 2026
HomeLanguagesHow to check if File Exists in PHP ?

How to check if File Exists in PHP ?

To check whether any file is existing or not then we can use the below-mentioned PHP function. To find the existence of the files, we use file_exists() function. This function is used to check whether a file or directory exists or not.

Syntax:

file_exists( $path )

Parameters: This function accept only one parameter $path. It specifies the path of the file or directory you want to check.

Return Value: It returns True on success and false on failure.

 
Errors And Exception:

  • The file_exists() function returns False if the path specified points to non-existent files.
  • For files larger than 2gb, some of the filesystem functions may give unexpected results since PHP’s integer type is signed and many platforms use 32bit integers.

Example: Suppose there exists a file named “file1.php”. Let’s check the existence of files “file1.php”.

PHP




<?php
    
$filename = "file1.php";
  
if (file_exists($filename)) {
    echo "File exist.";
} else {
    echo "File does not exist.";
}
  
?>


Output

File exist.
RELATED ARTICLES

5 COMMENTS

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12015 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6977 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS