Apparently flock() can't always be trusted. The PHP manual says this:
To acquire an exclusive lock (writer), set operation to LOCK_EX (set to 2 prior to PHP 4.0.1).
So if you call to a function something like this, expecting only one process to be able to append to the file at one time:
function WriteFile($FileName, $Data)
{
$FilePtr = fopen($FileName, "a") or die ("Couldn't open file : $FileName");
flock($FilePtr, LOCK_EX);
.
.
.
}
. . . you may think you are safe. But not quite. It seems that flock() only puts other processes on hold that may be writing to your file, until the file is closed. But it does not stop I/O from happening!
Huh? Isn't that a contradiction? Yes, it sure sounds like it to me. IOW, you may think the file is locked, and the other process is TOLD the file is locked, but it goes ahead and writes to the file anyway! That's just swell.
I don't know who designed flock() but I suspect they were either drugged out at the time, or else they had seen one to many flying saucers and were waiting for the Mother Ship :-)
Either way, flock() is basically useless. The only way to fix this is you have to sit there and manually grant a user the right to write to your file. Yes, I know. We all thought that was the purpose of flock(), but surprise!
This reminds of beginning air traffic control school in the Air Force. We had a very simple scheme for this: "Ok number 1, it's your turn to land . . . Ok number 2, you go next. . . Ok number 3, it's your turn!"