I'm a little confused about flock(). I have a script that adds a line to a log file each time it's accessed, and another script that allowes the occasional clearing of the log file (it deletes lines according to date).
For both scripts I'll need a write lock. flock needs a file pointer, which means fopen needs to be called previously, ie:
//$fp = fopen('log.log', 'w');
$fp = fopen('log.log', 'a');
if (flock($fp, LOCK_EX, TRUE)) {
...
}
Is this the correct way to go about it? Wouldn't using fopen 'w' truncate the file before the lock is obtained? Isn't there a chance another script instance could process between fopen and flock? Seems like a lock should be obtained before anything actually happens to the file.
Also, should the third flock param be TRUE or FALSE to cause flock to block (wait until a lock can be obtained)?