Efficiently tail a file from PHP - Reads the last x number of lines of a file (similar to Unix's tail command)
- Great performance
- It doesn't load the whole file to memory
- Skips trailing new lines and empty lines
- No dependencies
- Tested on Linux, Windows and macOS
- Compatible with PHP 7.4 and ^8
Usually used to read the last lines of:
- CSV files
- Log files
- JSON files
- text files
composer require jimenezmaximiliano/php-tail-file
$numberOfLines = 2;
$filePath = realpath("file.log");
$lines = Tail::tail($filePath, $numberOfLines);
// ["line 30", "line 31"]