我的代码:
function log_message($user = "", $message = "") {
$log_file = LOG_PATH;
// if we can open the file
if($handle = fopen($log_file, "a")) {
$timestamp = strftime("%Y-%m-%d:%H %H:%M:%S", time());
$content = $timestamp . " | [{$user}] | " . $message . "\n";
fwrite($handle, $content);
fclose($handle);
} else {
die("error opening log file: {$log_file}");
}
}
问题: 我第一次写文件没有问题,但任何后续的写入都没有通过并落入else语句?
其他信息: 该网站托管在godaddy上,我已将该文件夹的隐私权限设置为可写。
我尝试了什么: 我使用了函数
file_put_contents($log_file, $content . PHP_EOL, FILE_APPEND | LOCK_EX);
但结果相同。
我也尝试了这个问题中提到的解决方案:How to make sure a file handle has been closed before next operation?