How to read only 5 last line of the text file in PHP?

前端 未结 18 1654
陌清茗
陌清茗 2020-11-28 08:45

I have a file named file.txt which is update by adding lines to it.

I am reading it by this code:

$fp = fopen(\"file.txt\", \"r\");
$da         


        
18条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 09:24

    If you're on a linux system you could do this:

    $lines = `tail -5 /path/to/file.txt`;
    

    Otherwise you'll have to count lines and take the last 5, something like:

    $all_lines = file('file.txt');
    $last_5 = array_slice($all_lines , -5);
    

提交回复
热议问题