What are some common pitfalls to avoid when reading files in PHP?
One common pitfall to avoid when reading files in PHP is not checking if the file exists before trying to read it. This can lead to errors or unexpected behavior if the file is not found. To solve this issue, always use the file_exists() function to check if the file exists before attempting to read it.
$filename = 'example.txt';
if (file_exists($filename)) {
$file = fopen($filename, 'r');
$content = fread($file, filesize($filename));
fclose($file);
echo $content;
} else {
echo 'File not found';
}