【问题标题】:How to stop .txt from getting too big PHP如何阻止 .text 变得太大 PHP
【发布时间】:2019-10-09 11:20:14
【问题描述】:

您好,

我正在使用网站制作我的第一个 PHP,并且我正在将一些内容写入 log.txt。每次有人访问我的网站时,都会这样写:

$dateTime           = date('Y/m/d G:i:s');

$fh = fopen('log.txt', 'a');
fwrite($fh, 'Date / Time: '."".$dateTime ."\n\n");
fclose($fh);

现在我想知道,如何为我的 log.txt 设置最大文件大小以防止它变得太大。例如;它会自动删除最旧的内容“块”,比如说 6 行长,并在文件超过(例如)500 行后用新的内容替换它。

我在网上找不到这个问题,所以我很好奇我会怎么做。

如果您有任何问题,请告诉我,希望您能帮助我解决这个问题!

【问题讨论】:

  • 改为写入数据库。
  • 如果您使用的是网络服务器,可以在网络服务器日志中查看此信息以及更多信息
  • 我在 000webhost 上托管我的网站。但我问这个是因为我的一个朋友向我的网站发送了大约 40K 机器人,导致 log.txt 变得超过 130 000 行。
  • 看起来您正在重新发明日志轮换。 :-) 你有几个选择:自己写,或者在 log-rotation 上搜索一下
  • 嗯,好吧。我不想使用 composer,这只是一个单用户应用程序,网站不会吸引任何访问者。

标签: php logging


【解决方案1】:

请尝试此代码。我已经测试过它工作正常。我使用\r\n 换行,以便您的文本文件更具可读性。

$dateTime = date('Y/m/d G:i:s');
$fh = fopen('log.txt', 'a');
fwrite($fh, 'Date / Time: ' . "" . $dateTime . "\r\n");
fclose($fh);

现在您检查文件中的行数是否超出限制,然后从文件顶部删除旧行并在顶部输入新行,否则只需输入新行。

$block = 5; //block consist of 5 lines
$remove_blocks = 10; //remove the number of blocks
$remove = $block * $remove_blocks; //totle line to remove

$line_limit = 20;
$content = file_get_contents("log.txt");
$array = explode("\r\n", $content);
$count = count($array);
if ($count >= $line_limit) {

    //Remove first few lines
    $array = array_slice($array, $remove);
    $new_data = implode("\r\n", $array);

    $fh = fopen('log.txt', 'w');
    fwrite($fh, $new_data . "\r\n");
    fclose($fh);
} else {

    $dateTime = date('Y/m/d G:i:s');
    $fh = fopen('log.txt', 'a');
    fwrite($fh, 'Date / Time: ' . "" . $dateTime . "\r\n");
    fclose($fh);

}

编辑:将此代码放入新的 test.php 并进行实验

<?php

$block = 2; //block consist of 5 lines
$remove_blocks = 1; //remove the number of blocks
$remove = $block * $remove_blocks; //totle line to remove

$line_limit = 5;
$content = file_get_contents("log.txt");
$array = explode("\r\n", $content);
$array = array_slice($array, -1);
$count = count($array);
if ($count >= $line_limit) {

    //Remove first few lines
    $array = array_slice($array, $remove);
    $new_data = implode("\r\n", $array);

    $fh = fopen('log.txt', 'w');
    fwrite($fh, $new_data . "\r\n");
    fclose($fh);

    $dateTime = date('Y/m/d G:i:s');
    $fh = fopen('log.txt', 'a');
    fwrite($fh, 'Date / Time: ' . "" . $dateTime . "\r\n");
    fclose($fh);

} else {

    $dateTime = date('Y/m/d G:i:s');
    $fh = fopen('log.txt', 'a');
    fwrite($fh, 'Date / Time: ' . "" . $dateTime . "\r\n");
    fclose($fh);

}

?>

【讨论】:

  • 如何在删除前设置最大行数?我认为是 $line_limit = 20;但它现在在第 120 行,并没有删除任何内容
  • if ($count &gt;= $line_limit) { 这行检查限制然后$array = array_slice($array, $remove) 这行实际上是从数组中删除行。我认为您以前使用的是\n\n,所以这就是为什么它的行为不正确,将其替换为\r\n
  • 我认为这不是必需的,无论您想写入文件中的什么,您只需创建一个变量,然后将所有行与该变量连接以创建一个字符串,然后将该字符串写入文件并在字符串的末尾连接换行符,以便下次php可以检查换行符的位置。
  • 好的,是的,我有点慢,但确实有效。现在如果我想改变最大线怎么办?我将其更改为 200,但当它有 200 多行时它不会删除。 抱歉,我忘记更改它删除的块数。非常感谢您的帮助,我真的很感激!
  • $line = "line 1 \r\n"; $line .= " line 2 \r\n"; $line .= " line 3 \r\n"; 在这里,我正在创建一个包含 3 行的字符串 fwrite($fh, $line . "\r\n");.
【解决方案2】:

我建议为此使用“轮换日志文件”。谷歌对此进行了研究。你会得到一些简单的解决方案。

例如How to configure logrotate with php logs

【讨论】:

    【解决方案3】:

    这是一个如何从文件 https://www.w3resource.com/php-exercises/php-basic-exercise-16.php 获取行数的示例

    或者你可以尝试获取文件大小:

    if(filesize("log.txt") &gt;= 5000){ echo "file to is large"; }

    $content = file_get_contents("log.txt");
    $array = explode("\n", $content);
    $count = count($array);
    if($count >= 500){
    echo "file too large";
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用日期作为文件名来命名文件 所以基本上每天你都会有另一个文件

      $dateTime = date('Y/m/d G:i:s');
      //The file will have the name log_2019-10-09.txt
      $fh = fopen('log_'.date('Y-m-d').'.txt', 'a');
      fwrite($fh, 'Date/Time: '."".$dateTime ."\n\n");
      fclose($fh);
      

      【讨论】:

      • 这不是我真正想要的,但无论如何谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 2014-03-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多