PHP file_exists( ) Function - GeeksforGeeks

PHP file_exists( ) Function

Last Updated : 29 Oct, 2026

The file_exists() function in PHP checks whether a file or directory exists at the specified path. It returns true if the path exists and false otherwise.

Syntax:

file_exists(string $filename): bool

Parameter:

  • $filename: Specifies the path of the file or directory to check.

Return Value:

  • Returns true if the specified file or directory exists.
  • Returns false if the path does not exist or cannot be found.

Checks Whether a File Exists

The following example checks whether example.txt exists.

PHP
<?php
$file = "example.txt";

if (file_exists($file)) {
    echo "The file '$file' exists.";
} else {
    echo "The file '$file' does not exist.";
}
?>
  • The $file variable stores the filename.
  • file_exists() checks whether the specified path exists.
  • The if-else statement displays the appropriate message based on the result.

Output:

Screenshot-2026-10-29-135951

Checks Whether a Directory Exists

The file_exists() function also checks directory paths.

PHP
<?php
$directory = "uploads";

if (file_exists($directory)) {
    echo "The directory '$directory' exists.";
} else {
    echo "The directory '$directory' does not exist.";
}
?>
  • The $directory variable stores the directory path.
  • file_exists() checks whether the path exists.
  • The appropriate message is displayed based on the result.

Note: file_exists() checks both files and directories. Use is_file() to check specifically for a regular file and is_dir() to check specifically for a directory.

Output:

Screenshot-2026-10-29-140318
Comment