【问题标题】:PHP, How to read only couple of rows from filePHP,如何从文件中只读取几行
【发布时间】:2013-06-11 23:17:43
【问题描述】:

这是插入文件,一次插入 1 行。 例如,我只想读取最后 10 行。

$file = SITE_ROOT."logs/log.txt";

        if ($handle = fopen($file, 'a+b')) {
            $content = Session::get('username') . " has logged out on - " . datetime_to_text("now") . "\r\n";
            fwrite($handle, $content);
            fclose($handle);
        } else {
            echo 'Culd not open file for writing.';
        }

这就是我阅读它们的方式,阅读它们。

$file = SITE_ROOT . "logs/log.txt";
                                $content = "";
                                if ($handle = fopen($file, 'r')) {
                                    while (!feof($handle)) {
                                        $content .= '<li><a href="#">' . fgets($handle) . '</a></li>';
                                        echo count($handle);
                                    }
                                    fclose($handle);
                                }

                                echo nl2br($content);

如何只读取 10?

完成,我找到了我正在寻找的答案。

Thx, i found useffull this one.



$file = SITE_ROOT . "logs/log.txt";
$data = array_slice(file($file), -10);
$data1 = file($file);
foreach ($data as $line) {
echo '<li><a href="#">'. nl2br($line) . '</a></li>';
}

【问题讨论】:

标签: php fopen fclose


【解决方案1】:
 while (!feof($handle)) {

实际上是“读取”该行的行,因此将其更改为该行

$i=0;
$max=10;
while (!feof($handle) && ($i++ < $max)) {

设置一个计数器并计数,直到 $max 被击中。 抱歉,我错过了最后 10 行。只看到代码块之间的 10 行

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

提供最后 x 行的重要信息

【讨论】:

  • 是的,我想出了这个,但我想阅读 las 10 条目。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-27
  • 2015-10-23
相关资源
最近更新 更多