Are there any potential pitfalls to be aware of when using fwrite to modify a text file in PHP?

One potential pitfall when using fwrite to modify a text file in PHP is that it will completely overwrite the existing content of the file. To avoid this issue, you can read the existing content of the file, modify it as needed, and then write the updated content back to the file.

// Read the existing content of the file
$file = 'example.txt';
$currentContent = file_get_contents($file);

// Modify the content as needed
$newContent = $currentContent . "New line added.";

// Write the updated content back to the file
file_put_contents($file, $newContent);