Example 1 — Check If a File Exists
Check the current script, which always exists, and a file that does not exist.
<?php
$real = file_exists(__FILE__);
$missing = file_exists('missing-file.txt');
echo 'This script exists: ', var_export($real, true), PHP_EOL;
echo 'Missing file exists: ', var_export($missing, true), PHP_EOL; How It Works
- __FILE__ holds the full path of the script that is running.
- file_exists() returns true for it because the script is there.
- The second path points to a file that was never created, so the result is false.
- var_export() prints true and false as words instead of 1 and an empty string.

