background image

php 读取大文件最后几行数据的实现代码

分享一例

php 代码,用于读取大文件的后面几行数据,学习 fopen、fseek 等函数的用法,感

兴趣的朋友作个参考。

本节内容:
php 读取大文件内容
例子:

 

代码示例

:

<?php
/*
* 读取大文件最后几行数据
* by www.yl1001.com
*/
$file = $handledir.'/venocap.1'; 
    $fp = fopen($file, "r");  
    $line = 100; 
    $pos = -2; 
    $t = " "; 
    $data = ""; 
    while ($line > 0) { 
        while ($t != "\n") { 
            $flag=fseek($fp, $pos, SEEK_END); 
            if (fseek($fp, $pos, SEEK_END)==-1){ 
                //fseek($fp, 0); 
                rewind($fp); 
                $t="\n"; 
                $line=0; 
            }else{ 
                $t = fgetc($fp); 
                $pos --; 
            } 
        } 
        $t = " "; 
        $line --; 
    } 
    while (!feof($fp)) { 
        $data .= fgets($fp); 
        $data.='<br/>'; 
    } 
    fclose ($fp); 
echo $data