hexacyanide hexacyanide - 2 months ago 8
PHP Question

PHP: Retrieving lines from the end of a large text file

I've searched for an answer for quite a while, and haven't found anything that works correctly.

I have log files, some reaching

100MB
in size, around
140,000
lines of text.
With
PHP
, I am trying to get the last
500
lines of the file.

How would I get the
500
lines? With most functions, the file is read into memory, and that isn't a plausible case for this matter. I would preferably stay away from executing system commands.

Answer

If you are on a 'nix machine, you should be able to use shell escaping and the tool 'tail'. It's been a while, but something like this:

$lastLines = `tail -n 500`;

notice the use of tick marks, which executes the string in BASH or similar and returns the results.

Comments