How to read a large file line by line?

后端 未结 14 1480
[愿得一人]
[愿得一人] 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 00:57

    You can use the fgets() function to read the file line by line:

    $handle = fopen("inputfile.txt", "r");
    if ($handle) {
        while (($line = fgets($handle)) !== false) {
            // process the line read.
        }
    
        fclose($handle);
    } else {
        // error opening the file.
    } 
    

提交回复
热议问题