How to read a large file line by line?

后端 未结 14 1493
[愿得一人]
[愿得一人] 2020-11-22 00:21

I want to read a file line by line, but without completely loading it in memory.

My file is too large to open in memory, and if try to do so I always get out of memo

14条回答
  •  日久生厌
    2020-11-22 01:06

    This how I manage with very big file (tested with up to 100G). And it's faster than fgets()

    $block =1024*1024;//1MB or counld be any higher than HDD block_size*2
    if ($fh = fopen("file.txt", "r")) { 
        $left='';
        while (!feof($fh)) {// read the file
           $temp = fread($fh, $block);  
           $fgetslines = explode("\n",$temp);
           $fgetslines[0]=$left.$fgetslines[0];
           if(!feof($fh) )$left = array_pop($lines);           
           foreach ($fgetslines as $k => $line) {
               //do smth with $line
            }
         }
    }
    fclose($fh);
    

提交回复
热议问题