What are common pitfalls when using the file() function in PHP to read large log files?

When using the file() function in PHP to read large log files, a common pitfall is that it reads the entire file into an array, which can consume a lot of memory for large files. To avoid this issue, you can use the fopen() function to open the file in read mode and then read it line by line using fgets().

$filename = 'large_log_file.log';
$handle = fopen($filename, 'r');

if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // Process each line as needed
    }

    fclose($handle);
} else {
    echo 'Error opening file';
}