reading a block of lines in a file using php

后端 未结 5 1503
旧时难觅i
旧时难觅i 2020-12-20 03:16

Considering i have a 100GB txt file containing millions of lines of text. How could i read this text file by block of lines using PHP?

i can\'t use file_get_

5条回答
  •  遥遥无期
    2020-12-20 04:06

    i can't use file_get_contents(); because the file is too large. fgets() also read the text line by line which will likely takes longer time to finish reading the whole file.

    Don't see, why you shouldn't be able to use fgets()

    $blocksize = 50; // in "number of lines"
    while (!feof($fh)) {
      $lines = array();
      $count = 0;
      while (!feof($fh) && (++$count <= $blocksize)) {
        $lines[] = fgets($fh);
      }
      doSomethingWithLines($lines);
    }
    

    Reading 100GB will take time anyway.

提交回复
热议问题