What are common errors or pitfalls when trying to read the contents of a file in PHP?

Common errors when trying to read the contents of a file in PHP include not checking if the file exists before attempting to read it, not handling file permissions correctly, and not properly closing the file after reading it. To avoid these pitfalls, always check if the file exists, handle file permissions appropriately, and close the file after reading its contents.

$filename = 'example.txt';

if (file_exists($filename)) {
    $file = fopen($filename, 'r');
    
    if ($file) {
        $contents = fread($file, filesize($filename));
        fclose($file);
        
        echo $contents;
    } else {
        echo 'Error opening file.';
    }
} else {
    echo 'File does not exist.';
}