PHP删除文件的第一行,直到达到阈值[重复]。

问题描述 投票:0回答:1

我想把我的日志文件修剪为7KB,但修剪只运行一次,文件大小也不会更新。

下面是代码。

$thresholdKB = "7";
$files = glob('logs/*.{log}', GLOB_BRACE);

foreach($files as $file) {
    $filesizeKB = filesize("$file") / 1024;
    if ($filesizeKB > $thresholdKB){
        while ($filesizeKB > $thresholdKB) {
            $filesizeKB = filesize("$file") / 1024;

            $lines = file("$file"); 
            unset($lines[0]); 
            $fp = fopen("$file", 'w'); 
            fwrite($fp, implode('', $lines)); 
            fclose($fp);
        }
    }
}
php trim filesize
1个回答
0
投票

结果是 filesize 被缓存起来。你需要调用 clearstatcache() 更新文件后。

https:/www.php.netmanualenfunction.clearstatcache.php

© www.soinside.com 2019 - 2024. All rights reserved.