I have two different PHP files that both write to the same file. Each PHP script is called by a user action of two different HTML pages. I know it will be possible for the two PHP files to be called, but will both PHP files attempt to write to the file at the same time? If yes, what will happen? Also, it is possible to make one of the PHP fail gracefully (file write will just fail, and the other PHP can write to the file) as one PHP function is less important that the other.
Share
The usual way of addressing this is to have both scripts use
flock()for locking:This will cause the scripts to wait for each other to get done with the file before writing to it. If you like, the “less important” script can do:
so that it will just not do anything if it finds that something is busy with the file.