What is the workaround to prevent overwriting content when using fwrite in PHP?

When using fwrite in PHP to write content to a file, if the file already exists, the default behavior is to overwrite the existing content. To prevent this from happening, you can use the 'a' mode flag in combination with fopen, which will open the file for writing only if the file pointer is at the end of the file. This way, new content will be appended to the existing content instead of overwriting it.

$file = 'example.txt';
$content = "New content to be added.";

$handle = fopen($file, 'a');
fwrite($handle, $content);
fclose($handle);