What are some potential pitfalls to be aware of when reading files with PHP?

One potential pitfall when reading files with PHP is not properly handling errors that may occur during the file reading process. It is important to check for errors and handle them gracefully to prevent unexpected behavior in your application.

$file = 'example.txt';

$handle = fopen($file, 'r');

if ($handle === false) {
    die('Error opening file');
}

while (!feof($handle)) {
    $line = fgets($handle);
    // Process the line here
}

fclose($handle);