What is the potential issue with multiple users accessing and writing to the same text file in PHP?

When multiple users access and write to the same text file in PHP simultaneously, there is a risk of data corruption or conflicts due to overlapping writes. To prevent this issue, you can use file locking mechanisms to ensure that only one user can write to the file at a time. This will help maintain data integrity and prevent conflicts between multiple users.

$filename = 'data.txt';

$fp = fopen($filename, 'a+');
if (flock($fp, LOCK_EX)) {
    // Perform write operations here
    fwrite($fp, "New data\n");

    flock($fp, LOCK_UN); // Release the lock
} else {
    echo "Could not lock the file!";
}

fclose($fp);