What are the potential pitfalls of using file_exists() in PHP for checking the existence of a file?

Using file_exists() to check the existence of a file can lead to race conditions, where the file may be deleted or moved after the check but before the file is accessed. To avoid this issue, it is recommended to use file_get_contents() or fopen() with error suppression to check for the file's existence and access it in a single operation.

$file = 'example.txt';

if ($contents = @file_get_contents($file)) {
    // File exists and contents are fetched
    echo $contents;
} else {
    // File does not exist or cannot be accessed
    echo "File does not exist.";
}