What are the potential pitfalls of using fopen() to read a text file in PHP?

One potential pitfall of using fopen() to read a text file in PHP is not properly handling errors that may occur during the file opening process. To solve this issue, it is important to check if the file was successfully opened before attempting to read from it. This can be done by checking the return value of fopen() for a falsey value, indicating an error.

$filename = "example.txt";
$file = fopen($filename, "r");

if ($file) {
    // File opened successfully, proceed with reading
    $content = fread($file, filesize($filename));
    fclose($file);
    echo $content;
} else {
    // Error opening file, handle accordingly
    echo "Error opening file.";
}