2016-06-14 63 views

回答

1

简单的方法:

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);