PHPackages                             danil-kashin/file-lock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. danil-kashin/file-lock

ActiveLibrary[File &amp; Storage](/categories/file-storage)

danil-kashin/file-lock
======================

File-based exclusive locking via flock().

v1.0.0(2mo ago)0491BSD-3-ClausePHPPHP ^8.1

Since Mar 30Pushed 2mo agoCompare

[ Source](https://github.com/KashinDanil/FileLock)[ Packagist](https://packagist.org/packages/danil-kashin/file-lock)[ RSS](/packages/danil-kashin-file-lock/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (1)Versions (2)Used By (1)

FileLock
========

[](#filelock)

A lightweight PHP library for file-based exclusive locking using `flock()`.

Requirements
------------

[](#requirements)

- PHP 8.1 or higher

Installation
------------

[](#installation)

```
composer require danil-kashin/file-lock
```

Usage
-----

[](#usage)

### Basic acquire and release

[](#basic-acquire-and-release)

```
use DanilKashin\FileLock\FileLock;

$lock = new FileLock('/tmp/my-process.lock');

$lock->acquire(); // blocks until the lock is obtained
// ... critical section ...
$lock->release();
```

### Clean up the lock file after use

[](#clean-up-the-lock-file-after-use)

```
$lock = new FileLock('/tmp/my-process.lock');

$lock->acquire();
// ... critical section ...
$lock->release();
$lock->deleteFile();
```

### Error handling

[](#error-handling)

```
use DanilKashin\FileLock\Exceptions\FileLockException;

$lock = new FileLock('/tmp/my-process.lock');

try {
    $lock->acquire();
    // ... critical section ...
} catch (FileLockException $e) {
    // lock file could not be opened or the lock could not be acquired
    echo $e->getMessage();
} finally {
    $lock->release();
}
```

API
---

[](#api)

### `FileLock::__construct(string $lockFile)`

[](#filelock__constructstring-lockfile)

Creates a new `FileLock` instance. The `$lockFile` path is the file used as the lock. The file is created automatically on the first call to `acquire()` if it does not exist.

### `acquire(): void`

[](#acquire-void)

Acquires an exclusive lock (`LOCK_EX`). Blocks until the lock becomes available. Calling `acquire()` multiple times on the same instance is safe — the file handle is reused and `flock` is re-entrant on the same process.

Throws `FileLockException` if the lock file cannot be opened.

### `release(): void`

[](#release-void)

Releases the exclusive lock (`LOCK_UN`). The lock file itself is kept on disk.

### `deleteFile(): void`

[](#deletefile-void)

Deletes the lock file from disk. Call this after `release()` when the file is no longer needed.

How it works
------------

[](#how-it-works)

`FileLock` opens the target file in `cb` mode (create if missing, binary) and delegates all locking to the operating system via PHP's `flock()`. This makes it safe to use across multiple processes on the same machine.

Running tests
-------------

[](#running-tests)

```
composer install
vendor/bin/phpunit
```

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance83

Actively maintained with recent releases

Popularity11

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

87d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/808cc658e54eb99caa9573723ab97993e9fcd90d2b6cd0940a1ba7eb8950f8cf?d=identicon)[KashinDanil](/maintainers/KashinDanil)

---

Top Contributors

[![KashinDanil](https://avatars.githubusercontent.com/u/51259240?v=4)](https://github.com/KashinDanil "KashinDanil (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/danil-kashin-file-lock/health.svg)

```
[![Health](https://phpackages.com/badges/danil-kashin-file-lock/health.svg)](https://phpackages.com/packages/danil-kashin-file-lock)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
