What potential pitfalls should be considered when using the fopen function to check for file existence in PHP?

When using the fopen function to check for file existence in PHP, it's important to consider potential pitfalls such as file permissions, race conditions, and file path vulnerabilities. To mitigate these risks, it's recommended to use the file_exists function before attempting to open the file with fopen. This way, you can first check if the file exists and then proceed with opening it if necessary.

$filename = 'example.txt';

if (file_exists($filename)) {
    $file = fopen($filename, 'r');
    // Proceed with file operations
    fclose($file);
} else {
    echo 'File does not exist.';
}