As described in the docs, use LOCK_NB
to make a non-blocking attempt to obtain the lock, and on failure check the $wouldblock
argument to see if something else holds the lock.
if (!flock($fp, LOCK_EX|LOCK_NB, $wouldblock)) {
if ($wouldblock) {
// something already has a lock
}
else {
// couldn't lock for some other reason
}
}
else {
// lock obtained
}