我想要创建自动日志文件的基础上,每天一个月后,旧文件将被解除链接本身。以下是我的密码。我做得对吗。如果是,那么是否有办法减少磁盘上的文件大小。
$KeepDays = 30; # how many days of log files we'll keep on hand
$logname = date("Ymd") . '.status.log';
$OldFile = date('Ymd',mktime(0,0,0,date("m"),(date("d") - ($KeepDays + 1)),date("Y"))) . '.status.log';
if (file_exists($OldFile)) { unlink($OldFile); } # erase oldest log file
if (file_exists('logs/'.$logname)) {
$fp=fopen('logs/'.$logname,'a');
}else{
$fp=fopen('logs/'.$logname,'w');
}
$chunk = "Var1_".$var1.'_'.$Var2.' var3_'.$var3.' var_'.$var.' var_'.$var4 ;
fwrite($fp,$chunk. PHP_EOL);
fclose($fp);提前谢谢。
发布于 2014-11-18 07:33:28
别以为它会起作用。基本上删除旧文件,而不获取其内容。这些$var1等的定义是什么?有什么循环吗?
$KeepDays = 30; # how many days of log files we'll keep on hand
$logname = date("Ymd") . '.status.log';
// Would give '20141118.status.log ' for example
$OldFile = date('Ymd',mktime(0,0,0,date("m"),(date("d") - ($KeepDays + 1)),date("Y"))) . '.status.log';
// Would give '20141018.status.log' for example
if (file_exists($OldFile)) { unlink($OldFile); } # erase oldest log file
// Remove old file, but you remove it and can't get it's contents anymore. So you delete it wihtout copying its contents anywhere
if (file_exists('logs/'.$logname)) {
$fp=fopen('logs/'.$logname,'a');
}else{
$fp=fopen('logs/'.$logname,'w');
}
$chunk = "Var1_".$var1.'_'.$Var2.' var3_'.$var3.' var_'.$var.' var_'.$var4 ;
// Where are all those $var* defined? What you put here?
fwrite($fp,$chunk. PHP_EOL);
fclose($fp);此外,您还可以压缩或gzip文件内容。
https://stackoverflow.com/questions/26988499
复制相似问题