43 lines
827 B
PHP
43 lines
827 B
PHP
<?php
|
|
|
|
namespace Lean;
|
|
|
|
class File
|
|
{
|
|
public static function tail(string $path, int $lines = 10) : false|array
|
|
{
|
|
$handle = fopen($path, "r");
|
|
|
|
$counter = $lines;
|
|
|
|
$pos = -2;
|
|
$beginning = false;
|
|
$text = array();
|
|
|
|
while ($counter && ! $beginning) {
|
|
$t = "";
|
|
|
|
while ($t !== PHP_EOL) {
|
|
if(fseek($handle, $pos, SEEK_END) == -1) {
|
|
$beginning = true;
|
|
break;
|
|
}
|
|
|
|
$t = fgetc($handle);
|
|
$pos --;
|
|
}
|
|
|
|
$counter --;
|
|
|
|
if ($beginning) {
|
|
rewind($handle);
|
|
}
|
|
|
|
$text[$lines - $counter - 1] = fgets($handle);
|
|
}
|
|
|
|
fclose ($handle);
|
|
|
|
return array_reverse($text);
|
|
}
|
|
} |