我的日志文件有问题,过去我使用fopen($file, "w+");
生成w+
模式并在文件上写入,我使用fwrite
函数写入数据和标题。
随着时间的推移,我将模式更改为fopen($file, "a");
并使用该fputs
函数写入标题和fwrite
写入数据,但之后日志的大小与旧文件相比非常小,我认为我丢失了数据。
我的问题:我认为写入模式,阻止代码(有时)写入文件,这是真的吗?如果没有,我还需要检查其他任何事情来解决问题吗?
过去的代码:
if (file_exists($file)) {
$handle = fopen($file, "a+");
fwrite($handle, $record);
fclose($handle);
} else {
$handle = fopen($file, "w+");
$header = 'Name;Last Name;Age;User;Comment';
fwrite($handle, $header);
fwrite($handle, $record);
fclose($handle);
}
新代码:
if(!file_exists($file)) {
$handle = fopen($file, 'a');
$headers = ['Name', 'Last Name', 'Age', 'User', 'Comment'];
fputs($handle, implode($headers, ';')."\n");
fwrite($handle, $record);
fclose($handle);
} else {
$handle = fopen($file, "a+");
fwrite($handle, $record);
fclose($handle);
}
示例:过去,day 文件的大小为71Mo
,但在修改后,day 文件的大小为7Ko