Confirm buffer is not empty before erasing

Confirm file_exists before opening. For PHP 5.3 or earlier, check if
safe_mode is active to avoid errors.