How can the problem of overwriting existing content in a file be addressed when using "r+" mode in PHP file handling?

When using "r+" mode in PHP file handling, the problem of overwriting existing content in a file can be addressed by using the fseek() function to move the file pointer to the end of the file before writing new content. This ensures that the existing content is not overwritten, and the new content is appended to the end of the file.

$file = fopen("example.txt", "r+");

if ($file) {
    fseek($file, 0, SEEK_END);
    fwrite($file, "New content to be appended\n");
    fclose($file);
} else {
    echo "Error opening file.";
}