Skip to content
PHP file_exists() Function: Description, Parameters, Return Value, and Usage Examples

PHP file_exists() Function: Description, Parameters, Return Value, and Usage Examples

The article explains PHP's file_exists() function, detailing its syntax, parameter requirements, return values, safe‑mode warning, and provides two practical code examples demonstrating how to check for the existence of files or directories.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community

Function Overview The file_exists() function checks whether a specified file or directory exists and returns TRUE if it does, otherwise FALSE.

Signature bool file_exists(string $filename) Parameter $filename – the path to the file or directory. For Windows network shares use //computername/share/filename or \\computername\share\filename.

Return Value Returns TRUE when the file or directory exists; returns FALSE otherwise.

Warning If safe mode restricts access to the file, the function will return FALSE. In such cases, include can be used if the file resides in the directory specified by safe_mode_include_dir.

Example 1 – Conditional Check

<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?>

Example 2 – Direct Call

<?php
    echo file_exists("test.txt");
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendFilesystemphp-functionsfile_exists
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.