Found in the open code. It is very convenient and works quickly even with large files, but I failed to display more than one line. Is it possible for them to display the last 10 lines of the file?

<?php $f = fopen("email.log", "r"); if($f){ if(fseek($f, -1, SEEK_END) == 0){//Π² ΠΊΠΎΠ½Π΅Ρ† Ρ„Π°ΠΉΠ»Π° -1 символ ΠΏΠ΅Ρ€Π΅Π²ΠΎΠ΄Π° строки $len = ftell($f); for($i = $len; $i > ($len-5000); $i--){//5000 - прСдполагаСмая макс. Π΄Π»ΠΈΠ½Π° строки fseek($f, -2, SEEK_CUR); if(fread($f,1) == "\n")//Ссли встрСтился ΠΏΡ€ΠΈΠ·Π½Π°ΠΊ ΠΊΠΎΠ½Ρ†Π° строки break; } echo 'line start:',$i, '<br>'; echo fread($f, $len - $i);//послСдняя строка } fclose($f); } 
  • make a queue for the last ten lines and output them at the end of the file - splash58
  • I tried to make a queue. - Seryoga Bulygin
  • Cuts the last but one line in the middle. <? php $ f = fopen ("email.log", "r"); if ($ f) {if (fseek ($ f, -2, SEEK_END) == 0) {// at the end of the file -1 newline $ len = ftell ($ f); for ($ i = $ len; $ i> ($ len-5000); $ i -) {// 5000 - estimated max. fseek string length ($ f, -3, SEEK_CUR); if (fread ($ f, 1) == "\ n") // if a line terminator is encountered break; } echo '<br>'; echo fread ($ f, $ len - $ i); // last line} fclose ($ f); }?> - Seryoga Bulygin
  • you better place in response - here is not readable at all - splash58

1 answer 1

 <?php $f = fopen("email.log", "r"); if($f){ if(fseek($f, -2, SEEK_END) == 0){//Π² ΠΊΠΎΠ½Π΅Ρ† Ρ„Π°ΠΉΠ»Π° -2 символ ΠΏΠ΅Ρ€Π΅Π²ΠΎΠ΄Π° строки $len = ftell($f); //ftell β€” Π‘ΠΎΠΎΠ±Ρ‰Π°Π΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰ΡƒΡŽ ΠΏΠΎΠ·ΠΈΡ†ΠΈΡŽ чтСния/записи Ρ„Π°ΠΉΠ»Π° for($i = $len; $i > ($len-5000); $i--){//5000 - прСдполагаСмая макс. Π΄Π»ΠΈΠ½Π° строки fseek($f, -3, SEEK_CUR); if(fread($f,1) == "\n")//Ссли встрСтился ΠΏΡ€ΠΈΠ·Π½Π°ΠΊ ΠΊΠΎΠ½Ρ†Π° строки break; } echo fread($f, $len - $i);//послСдняя строка echo '<br>'; } fclose($f); } ?> 

This code prints half the penultimate line.