PHP read last few lines of large file

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/19
 * Time: 10:39
 */
function tail($filename, $num)
{
if (!$fp = fopen($filename, 'r')) {
echo "open file fail";
return false;
    }
$pos = -2;
$eof = "";
$str = "";
while ($num > 
                                    0 ) {
 while ( $eof != " \n " ) { // This control starts reading from the last line of the file
 if (! fseek ( $fp , $pos , SEEK_END )) {
 $eof = fgetc ( $fp );
 $pos --;
             } else {
 break ;
             }
         }
 $str .= fgets ( $fp ); // Read a line of data
 $eof = "" ;
 $num                                                                                            --;
    }
return $str;
}
$filename = dirname(__FILE__) . "\\files\\laravel-2018-04-09.log";
echo nl2br(tail($filename, 4));    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324644726&siteId=291194637
Recommended