There are multiple ways to read files with PHP. You can use file(), file_get_contents(), fsockopen(), or fopen() functions. If you want to read through a file line by line, though, you will want to use one of the latter two and use their pointer to navigate through your file.

I found the following simple commands very helpful to iterate through a large log file and collect specific information out of it. It has many uses, so I will keep this as vanilla as possible.

In my situation, I ended up using substr() to look for specific log messages and pluck out some key data I needed.

The following simple script will setup each line of the file in the $line variable. You can put your own code below that to perform whatever work you need.

Open a file and read one line at a time

	$file = fopen("path/to/my/file.log", 'r');
	while( !feof( $file ) )
		$line = fgets($file);
		/* Do your work here */

Note: I used the ‘r’ flag on the fopen function. This is purely for reading the file. You can use ‘r+’ if you would like to edit the file, or consult the PHP manual on fopen for all the flags available.

If you have any questions, let me know below!