What are some common pitfalls when using fopen to read files in PHP?

One common pitfall when using fopen to read files in PHP is not checking if the file exists before attempting to open it. This can lead to errors or unexpected behavior if the file is not found. To avoid this issue, you should always check if the file exists using the file_exists function before trying to open it with fopen.

$file_path = 'example.txt';

if (file_exists($file_path)) {
    $file = fopen($file_path, 'r');
    
    // Read file contents here
    
    fclose($file);
} else {
    echo 'File does not exist.';
}