18

我猜它是 fgets,但我找不到具体的语法。我正在尝试读出(在我认为更容易的字符串中)添加到日志文件的最后一行。

4

9 回答 9

47

最简单的天真的解决方案很简单:

$file = "/path/to/file";
$data = file($file);
$line = $data[count($data)-1];

不过,这会将整个文件加载到内存中。可能是一个问题(或不是)。一个更好的解决方案是:

$file = escapeshellarg($file); // for the security concious (should be everyone!)
$line = `tail -n 1 $file`;
于 2009-06-30T09:40:03.307 回答
14

这看起来就像您正在寻找的内容:

tekkie.flashbit.net:PHP 中的尾部功能

它实现了一个函数,该函数使用带有负索引的 fseek() 从末尾卷起文件。您可以定义要返回的行数。

该代码也可作为 Gist 在 GitHub 上获得:

// full path to text file
define("TEXT_FILE", "/home/www/default-error.log");
// number of lines to read from the end of file
define("LINES_COUNT", 10);


function read_file($file, $lines) {
    //global $fsize;
    $handle = fopen($file, "r");
    $linecounter = $lines;
    $pos = -2;
    $beginning = false;
    $text = array();
    while ($linecounter > 0) {
        $t = " ";
        while ($t != "\n") {
            if(fseek($handle, $pos, SEEK_END) == -1) {
                $beginning = true; 
                break; 
            }
            $t = fgetc($handle);
            $pos --;
        }
        $linecounter --;
        if ($beginning) {
            rewind($handle);
        }
        $text[$lines-$linecounter-1] = fgets($handle);
        if ($beginning) break;
    }
    fclose ($handle);
    return array_reverse($text);
}

$fsize = round(filesize(TEXT_FILE)/1024/1024,2);

echo "<strong>".TEXT_FILE."</strong>\n\n";
echo "File size is {$fsize} megabytes\n\n";
echo "Last ".LINES_COUNT." lines of the file:\n\n";

$lines = read_file(TEXT_FILE, LINES_COUNT);
foreach ($lines as $line) {
    echo $line;
}
于 2009-06-30T09:39:35.197 回答
9
define('YOUR_EOL', "\n");
$fp = fopen('yourfile.txt', 'r');

$pos = -1; $line = ''; $c = '';
do {
    $line = $c . $line;
    fseek($fp, $pos--, SEEK_END);
    $c = fgetc($fp);
} while ($c != YOUR_EOL);

echo $line;

fclose($fp);

这更好,因为它不会将完整的文件加载到内存中......

将 YOUR_EOL 设置为正确的行尾,如果您使用与脚本所在操作系统的默认行尾相同的行尾,则可以使用常量 PHP_EOL。

于 2009-06-30T10:14:58.713 回答
4
function seekLastLine($f) {
    $pos = -2;
    do {
        fseek($f, $pos--, SEEK_END);
        $ch = fgetc($f);
    } while ($ch != "\n");
}

-2因为最后一个字符可以\n

于 2012-01-16T10:22:44.743 回答
3

您要么必须逐行读取文件并保存最后读取的行以获取它。

或者,如果在 unix/linux 上,您可能会考虑使用 shell 命令 tail

tail -n 1 filename
于 2009-06-30T09:38:34.513 回答
1

如果要逐行读取文件,该file函数会逐行读取文件的内容,并将每一行作为数组的元素返回。

所以你可以做一些简单的事情,比如:

$lines    = file('log.txt');
$lastLine = array_pop($lines);
于 2009-06-30T09:39:45.337 回答
1

这不会因 1 或 0 行文件而中断。

function readlastline($fileName)
{

       $fp = @fopen($fileName, "r");
       $begining = fseek($fp, 0);      
       $pos = -1;
       $t = " ";
       while ($t != "\n") {
             fseek($fp, $pos, SEEK_END);
             if(ftell($fp) == $begining){
              break;
             }
             $t = fgetc($fp);
             $pos = $pos - 1;
       }
       $t = fgets($fp);
       fclose($fp);
       return $t;
}
于 2009-10-29T04:57:47.790 回答
1

...为什么只读最后一行?

function readLines($fp, $num) {

    $line_count = 0; $line = ''; $pos = -1; $lines = array(); $c = '';

    while($line_count < $num) {
        $line = $c . $line; 
        fseek($fp, $pos--, SEEK_END);
        $c = fgetc($fp);
        if($c == "\n") { $line_count++; $lines[] = $line; $line = ''; $c = ''; }
    }   
    return $lines;
}

$filename = "content.txt";
$fp = @fopen($filename, "r");

print_r(readLines($fp, 2));

fclose($fp);
于 2012-01-30T10:35:34.523 回答
0

@unique_stephen,您的答案有缺陷。PHP fseek 成功返回 0,失败返回 -1。将结果存储在 $begining (sic) 中,然后在 ftell() 的过滤器中使用它是不正确的。如果我的声誉更高,我会投票给你并留下评论。这是 unique_stephen 函数的修改版本。

function readlastline($fileName)
{
    $fp = @fopen($fileName, "r");
    if (fseek($fp, 0) == -1)
        exit('Cannot seek to beginning of the file'); 
    $pos = -1;
    $t = " ";
    while ($t != "\n") {
        if (fseek($fp, $pos, SEEK_END) == -1)
            exit('Cannot seek to the end of the file');
        if (ftell($fp) == 0) {
            break;
        }
        $t = fgetc($fp);
        $pos = $pos - 1;
    }
    $t = fgets($fp);
    fclose($fp);
    return $t;
}

注意:PHP 的 fseek 无法设法找到大于 PHP_MAX_INT 的文件的末尾,即使在 64 位二进制文​​件上也是 32 位签名的。

于 2018-02-24T16:13:40.843 回答