title: PHP - how to read big remote files efficiently and use buffer in loop
body: <p>i would like to understand how to use the buffer of a read file.</p>
  
  <p>Assuming we have a big file with a list of emails line by line ( delimiter is a classic <code>\n</code> )</p>
  
  <p>now, we want compare each line with each record of a table in our database in a kind of check like <code>line_of_file == table_row</code>.</p>
  
  <p>this is a simple task if you have a normal file, otherwise, if you have a huge file the server usually stop the operation after few minute.</p>
  
  <p>so what's the best way of doing this kind of stuff with the file buffer?</p>
  
  <p>what i have so far is something like this:</p>
  
  <pre><code>$buffer = file_get_contents('file.txt');
  while($row = mysql_fetch_array($result)) {
    if ( preg_match('/'.$email.'/im',$buffer)) {
      echo $row_val;
    }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>$buffer = file_get_contents('file.txt');
  $lines = preg_split('/\n/',$buffer); 
  //or $lines = explode('\n',$buffer);
  while($row = mysql_fetch_array($result)) {
    if ( in_array($email,$lines)) {
      echo $row_val;
    }
  }
  </code></pre>