How to process a file line by line in PHP?

Posted on In QA

In PHP, how to process a file line by line? The file is a plain text line like input.txt. As an example, the process can be to just print it out.

In PHP, you can use this code snippet to process a file line by line:

if ( ($fhandle = fopen("./input.txt", "r") !== FALSE ) {
  while (($line = fgets($fhandle)) !== false) {
    echo $line
  }

  fclose($handle);
} else {
  // error handling
} 

Leave a Reply

Your email address will not be published. Required fields are marked *