What are common pitfalls to avoid when reading text files in PHP?

Common pitfalls to avoid when reading text files in PHP include not checking if the file exists before attempting to read it, not handling errors or exceptions that may occur during the file reading process, and not properly closing the file after reading it to free up system resources.

// Check if the file exists before attempting to read it
$file = 'example.txt';
if (file_exists($file)) {
    // Open the file for reading
    $handle = fopen($file, 'r');
    
    // Read the contents of the file
    $contents = fread($handle, filesize($file));
    
    // Close the file
    fclose($handle);
    
    // Output the contents of the file
    echo $contents;
} else {
    echo 'File does not exist.';
}