Simultaneous File Access

php

Solution

Small-scale file operations are so quick that two writes at the exact same time are fairly rare. Regardless, you can use `flock` to lock the file:

$fp = fopen('file.log', 'a+');
flock($fp, LOCK_EX);
fwrite($fp, 'my data');
flock($fp, LOCK_UN);
fclose($fp);

Note `fclose` automatically unlocks the file, but the I find it makes the code a little more user-friendly to put these things in.

Problem

Using: ``` fopen fwrite fclose ``` What happens if two users attempt to open the same file at the same time?

Original source