Conditions | 5 |
Paths | 8 |
Total Lines | 23 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
31 | public function getLastLines(int $lines = 10, $buffer = 4096) : string { |
||
32 | $f = fopen($this->full_path, "rb"); |
||
33 | fseek($f, -1, SEEK_END); |
||
|
|||
34 | if(fread($f, 1) != "\n") { |
||
35 | $lines -= 1; |
||
36 | } |
||
37 | $output = ''; |
||
38 | $chunk = ''; |
||
39 | |||
40 | while(ftell($f) > 0 && $lines >= 0) { |
||
41 | $seek = min(ftell($f), $buffer); |
||
42 | fseek($f, -$seek, SEEK_CUR); |
||
43 | $output = ($chunk = fread($f, $seek)).$output; |
||
44 | fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR); |
||
45 | $lines -= substr_count($chunk, "\n"); |
||
46 | } |
||
47 | |||
48 | while($lines++ < 0) { |
||
49 | $output = substr($output, strpos($output, "\n") + 1); |
||
50 | } |
||
51 | |||
52 | fclose($f); |
||
53 | return $output; |
||
54 | } |
||
60 | } |