You can use the PHP flock function to lock the file for writing, but this lock will not be saved on the Internet of requests and does not work on NFS mounts (at least in my experience).
It is best to create a text file in the same directory, check its existence and report an error if it exists.
As with any blocking scheme, you will have race and blocking conditions that remain after the operation is completed, so you will need a way to reduce them.
I would recommend creating a hash of the file before editing and saving this value in the lock file. Also send this hash to the client as part of the edit form (so that it returns as data in the commit request). Before writing, compare the past hash value with the value in the file. If they match, commit the data and remove the lock.
If they are different, show a mistake.
source share