How to read a large file line by line?

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

    Use buffering techniques to read the file.

    $filename = "test.txt";
    $source_file = fopen( $filename, "r" ) or die("Couldn't open $filename");
    while (!feof($source_file)) {
        $buffer = fread($source_file, 4096);  // use a buffer of 4KB
        $buffer = str_replace($old,$new,$buffer);
        ///
    }
    

提交回复
热议问题