How to append data to an existing file in PHP?

To append data to an existing file in PHP, you can use the file_put_contents() function with the FILE_APPEND flag set. Here's an example:

$data = "New data to append"; // Append data to the file file_put_contents("filename.txt", $data, FILE_APPEND | LOCK_EX); echo "Data appended to the file!";

In this example, the file_put_contents() function appends the $data to the file specified by the filename ("filename.txt"). The FILE_APPEND flag ensures that the data is appended to the existing content instead of overwriting it. The LOCK_EX flag is used to acquire an exclusive lock on the file to prevent simultaneous writes from multiple processes.

After appending the data, you can display a message or perform any other necessary actions.