Reputation: 6307
I want to delete the contents of the error log before running error_log();
to put new errors.
How may I do this from within a PHP file?
Upvotes: 2
Views: 621
Reputation: 2935
Simple way:
file_put_contents(ini_get('error_log'), '');
error_log("Houston, we have a problem.", 0);
This will work only, if your user that is executing this has the permission to do this (Write access to this file).
Otherwise you can set a other location for your runtime - in a location where you have access for this log. Like this:
ini_set('error_log', __DIR__ . DIRECTORY_SEPARATOR . 'error.log');
# ^ full path with leading slash
If you want to backup the file instead you can do this with that 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 explained/expanded:
$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);
Upvotes: 3