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

前端 未结 18 1625
陌清茗
陌清茗 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:08

    I've tested this one. It works for me.

    function getlast($filename,$linenum_to_read,$linelength){
    
       // this function takes 3 arguments;
    
    
       if (!$linelength){ $linelength = 600;}
    $f = fopen($filename, 'r');
    $linenum = filesize($filename)/$linelength;
    
        for ($i=1; $i<=($linenum-$linenum_to_read);$i++) {
        $data = fread($f,$linelength);
        }
    echo "
    ";       
        for ($j=1; $j<=$linenum_to_read+1;$j++) {
        echo fread($f,$linelength);
        }
    
    echo "

    The filesize is:".filesize("$filename"); } getlast("file.txt",6,230); ?>

提交回复
热议问题