Home > Software design >  How to read portions of a big gz file with PHP
How to read portions of a big gz file with PHP

Time:11-28

I need to read a big txt file (more than 1GB) in order to extract multiple strings inside it. Using gzread, I'm able to read from the beginning to an amount of kb (let's say 1000000). But how can I read the next 1000000kb of the file? Is there a way to start from a point and read 1000000kb from there until the file is not finished?

$filename = "my_big_file.txt.gz";
$zd = gzopen($filename, "r");
$readfile = gzread($zd,1000000); 
$first_array = explode("IT\\",$readfile);
unset($first_array[0]);
foreach($first_array as $row){
  $full_id = explode(" ",$full_id);
  echo $full_id[0];
  echo "<br>";
}
gzclose($zd);

CodePudding user response:

If you mean: can you start reading the second million bytes of a gzip file without having read the first million bytes, then the answer is no. You cannot randomly access a gzip file.

CodePudding user response:

Place the gzread in a while loop, like so:

<?php

$filename = "my_big_file.txt.gz";
$zd = gzopen($filename, "r");
while($readfile = gzread($zd,1000000)) {
    //...your process
}
gzclose($zd);
  • Related