Supported Versions: PHP 4, PHP 5, PHP 7, PHP 8
Portable advisory file locking
<?php flock(resource $stream, int $operation, int &$would_block = null): bool
<?php if ($handle) {
try {
if (flock($handle, LOCK_SH)) {
clearstatcache(true, $path);
$contents = fread($handle, $this->size($path) ?: 1);
<?php $contents = fread($handle, $this->size($path) ?: 1);
flock($handle, LOCK_UN);
}
} finally {
fclose($handle);
<?php public function getSharedLock($block = false)
{
if (! flock($this->handle, LOCK_SH | ($block ? 0 : LOCK_NB))) {
throw new LockTimeoutException("Unable to acquire file lock at path [{$this->path}].");
}
<?php public function getExclusiveLock($block = false)
{
if (! flock($this->handle, LOCK_EX | ($block ? 0 : LOCK_NB))) {
throw new LockTimeoutException("Unable to acquire file lock at path [{$this->path}].");
}
<?php public function releaseLock()
{
flock($this->handle, LOCK_UN);
$this->isLocked = false;
<?php if (! flock($handle, LOCK_EX | LOCK_NB)) {
return false;
}