我想在运行 error_log();
之前删除错误日志的内容以放置新错误。
如何在 PHP 文件中执行此操作?
最佳答案
简单的方法:
file_put_contents(ini_get('error_log'), '');
error_log("Houston, we have a problem.", 0);
仅当执行此操作的用户有权执行此操作(对此文件的写访问权限)时,此操作才有效。
否则你可以set a other location for your runtime - 在您有权访问此日志的位置。像这样:
ini_set('error_log', __DIR__ . DIRECTORY_SEPARATOR . 'error.log');
# ^ full path with leading slash
如果您想备份文件,您可以使用该 oneliner 来执行此操作:
file_put_contents(pathinfo(ini_get('error_log'), PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR . 'php_error_backup_'.time().'.log', file_get_contents(ini_get('error_log')));
Oneliner 解释/扩展:
$save_location = pathinfo(ini_get('error_log'), PATHINFO_DIRNAME);
$backup_filename = $save_location . DIRECTORY_SEPARATOR . 'php_error_backup_'.time().'.log';
$backup_content = file_get_contents(ini_get('error_log'));
file_put_contents($backup_filename, $backup_content);
关于php - 如何从 php 文件中删除错误日志内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37807659/