I have a website where at the same moment, there can be multiple users writing to the same file at the same time. An example of my code is below.
PHP 5.6
You need to change saving mechanism from simple file into any database with transactions.