Get last line of file in PHP

Here is a simple way to get the last line of a file without reading the entire file into memory.

Use Linux’s Tail command to get last line of a file.

[source language=“php“]
$file = escapeshellarg( ‚/home/bin/somefile.txt‘ );
$last_line = `tail -n 1 $file`;
[/source]

[Update]
Here is a PHP fseek function that utilises a dynamic buffer which is more efficient than tail on larger files – https://gist.github.com/lorenzos/1711e81a9162320fde20

3 Kommentare zu „Get last line of file in PHP

Antworte auf den Kommentar von EoinAntwort abbrechen