What are some potential pitfalls when trying to read a large .gz file in PHP?

When trying to read a large .gz file in PHP, one potential pitfall is running out of memory due to trying to read the entire file into memory at once. To avoid this issue, it is recommended to read the file line by line or in chunks to prevent memory exhaustion.

$filename = 'large_file.gz';
$handle = gzopen($filename, 'r');

if ($handle) {
    while (!gzeof($handle)) {
        $chunk = gzread($handle, 1024); // Read 1KB at a time
        // Process the chunk here
    }

    gzclose($handle);
}