file_exists()
file_exists() — Checks whether a file or directory exists.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
file_exists(string $filename): bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $filename | string | Path to the file or directory. |
Return value
Returns true if the file or directory specified by filename exists; false otherwise. Note: This function will return false for symlinks pointing to non-existing files. This function will return false for symlinks pointing to non-existing files. Note: The check is done using the real UID/GID instead of the effective one. The check is done using the real UID/GID instead of the effective one. Note: Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB. Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results ...
Example
A runnable snippet:
$filename = '/path/to/foo.txt'; if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; }
See also
is_readable(), is_writable(), is_file(), file()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page file_exists() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.