What are the potential pitfalls of appending data to a file using fwrite in PHP?

Appending data to a file using fwrite in PHP can lead to potential pitfalls such as overwriting existing data in the file or not properly handling file permissions. To avoid these issues, it's important to use the "a" mode flag when opening the file, which will append data to the end of the file without truncating it. Additionally, it's crucial to check for errors when writing to the file to ensure that the operation was successful.

$file = 'data.txt';
$data = "New data to append\n";

$handle = fopen($file, 'a');

if ($handle === false) {
    die("Cannot open file for appending");
}

if (fwrite($handle, $data) === false) {
    die("Cannot write to file");
}

fclose($handle);