PHP - how to efficiently read large deleted files and use a buffer in a loop

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

Assuming we have a large file with a list of letters per line (classic separator \n)

Now, we want to compare each row with each table entry in our database as a type check line_of_file == table_row.

This is a simple task if you have a normal file, otherwise, if you have a huge file, the server usually stops the operation after a few minutes.

so what's the best way to do this stuff with a file buffer?

that I still have something like this:

$buffer = file_get_contents('file.txt');
while($row = mysql_fetch_array($result)) {
  if ( preg_match('/'.$email.'/im',$buffer)) {
    echo $row_val;
  }
}

$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;
  }
}
+3
source share
3 answers

As already suggested in my comments on your question (hence CW):

SplFileObject, Iterator . .

.

+3

file_get_contents . .

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

fopen() . , fgets().

file_get_contents , ,

, , PHP , , - 2 .

:

  • set_time_limit(0), PHP
  • It is imperative to display some data every 30 seconds or so that the browser does not time out; make sure flush();and maybe that ob_flush();is why your output is actually sent over the network (this is kludge)
  • start a separate process (for example, through exec()) to start it in the background. Honestly, anything that takes more than a second or two is best run in the background.
+1
source

All Articles