PHP - Preventing collision in Cron - File lock safe?

In Symfony Framework you could use the lock component symfony/lock

https://symfony.com/doc/current/console/lockable_trait.html


This sample was taken at http://php.net/flock and changed a little and this is a correct way to do what you want:

$fp = fopen("/path/to/lock/file", "w+");
if (flock($fp, LOCK_EX | LOCK_NB)) { // do an exclusive lock
  // do the work
  flock($fp, LOCK_UN); // release the lock
} else {
  echo "Couldn't get the lock!";
}
fclose($fp);

Do not use locations such as /tmp or /var/tmp as they could be cleaned up at any time by your system, thus messing with your lock as per the docs:

Programs must not assume that any files or directories in /tmp are preserved between invocations of the program.

https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s18.html https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s15.html

Do use a location that is under your control.

Credits:

  • Michaël Perrin - for proposing to use w+ instead of r+