我无法附加到我的日志文件中。如果我的日志文件超过50 MB,则创建一个新的日志文件。如果没有,则附加到上一个文件。我不确定我是否正确使用FILE_APPEND。最好使用fopen和fwrite吗?
以下是我的代码:
//this is the file to test size to determine whether to append to it or start a new log file
if ($latestFile != '') {
$latestFileSize = filesize($latestFile);//file size in bytes (1/1000000 of MB)
}
if ($latestFileSize != 0) {
$fileSizeThreshold = 50 * 1000000;//Threshold for log file size limit in bytes (50 MB)
if ($latestFileSize > $fileSizeThreshold) {
//The latest results file deletion log file is over 50 MB in size, so create a new one
file_put_contents("c:\\sites\\{$logFileName}", $log);
} else {
//2. Troubleshoot why the FILE_APPEND is not working and fix it. What is the normal behavior of the FILE_APPEND?
//The latest results file deletion log file is NOT over 50 MB in size, so append log entry to latest one
file_put_contents("c:\\sites\\{$latestFileName}", $log, FILE_APPEND);
}
} else {
//This is the first file in the log directory. Create it.
file_put_contents("c:\\sites\\{$logFileName}", $log);
}
closedir($handle);
}//end if $handle
答案 0 :(得分:0)
您确定$latestFileSize
并非总是== 0吗?
file_put_contents
通常没有问题,首先对逻辑进行故障排除。确保您使用FILE_APPEND访问file_put_contents
,因为您使用它的方式非常好。