PHP - how to read big remote files efficiently and use buffer in loop

后端 未结 3 1886
礼貌的吻别
礼貌的吻别 2021-01-21 04:53

i would like to understand how to use the buffer of a read file.

Assuming we have a big file with a list of emails line by line ( delimiter is a classic \\n

3条回答
  •  爱一瞬间的悲伤
    2021-01-21 05:36

    Don't use file_get_contents for large files. This pulls the entire file into memory all at once. You have to read it in pieces

    $fp = fopen('file.txt', 'r');
    while(!feof($fp)){
      //get onle line 
      $buffer = fgets($fp);
       //do your stuff
    }
     fclose($fp);
    

提交回复
热议问题