PHP flock() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, flock() locks a file so that only one script can write to it at a time. This is called advisory file locking.

If two visitors run the same script together, both may try to write to one file and corrupt it. flock() makes the second one wait until the first one is finished.

Syntax

Call flock() with the values you need. Start with this line.

PHP
flock($handle, LOCK_EX);
CallWhat it does
flock($fp, LOCK_SH)Takes a shared lock, so many scripts can read at once while nobody writes.
flock($fp, LOCK_EX)Takes an exclusive lock for writing, and waits if another script holds a lock.
flock($fp, LOCK_EX | LOCK_NB, $wouldBlock)Tries to lock without waiting and tells you if the file is busy.
flock($fp, LOCK_UN)Releases the lock.

The lock is advisory, which means it only works if every script that uses the file also calls flock(). Closing the file with fclose() also releases the lock. Do not use flock() on network file systems such as NFS unless you know they support it.

Library declarations
PHP
flock(resource $stream, int $operation, int &$would_block = null): bool

Parameters

ParameterDescription
streamRequired. An open file handle returned by fopen() or tmpfile().
operationRequired. LOCK_SH for a shared lock, LOCK_EX for an exclusive lock, or LOCK_UN to release. Add LOCK_NB to avoid waiting.
would_blockOptional. A variable that is set to 1 when the lock is blocked by another process.

Return Value

Return ValueDescription
boolReturns true on success or false on failure, for example when LOCK_NB is used and the file is already locked.

PHP Examples

Example 1 — Write with an Exclusive Lock

Lock a file, write to it, and release the lock again.

PHP
<?php
$file = __DIR__ . '/lock_demo.txt';
$fp = fopen($file, 'c');

if (flock($fp, LOCK_EX)) {
    ftruncate($fp, 0);
    fwrite($fp, 'Saved safely');
    fflush($fp);
    flock($fp, LOCK_UN);
    echo "Written with a lock", PHP_EOL;
}

fclose($fp);
unlink($file);

How It Works

  1. fopen() with the c mode opens the file for writing and creates it if it is missing.
  2. flock() with LOCK_EX waits until no other script holds a lock, then locks the file.
  3. ftruncate() clears old content, fwrite() saves the new text, and fflush() pushes it to the disk.
  4. flock() with LOCK_UN releases the lock so another script can use the file.

Example 2 — Do Not Wait for a Busy File

Add LOCK_NB to try the lock without waiting. Here a second handle finds the file already locked.

PHP
<?php
$file = __DIR__ . '/busy.txt';

$first  = fopen($file, 'c');
$second = fopen($file, 'c');

flock($first, LOCK_EX);

$ok = flock($second, LOCK_EX | LOCK_NB, $wouldBlock);

echo $ok ? 'Got the lock' : 'File is busy', PHP_EOL;
echo "Would block: $wouldBlock", PHP_EOL;

fclose($first);
fclose($second);
unlink($file);

How It Works

  1. Two handles open the same file, which is like two scripts using it at once.
  2. The first handle takes an exclusive lock.
  3. The second handle tries with LOCK_NB, so it does not wait and returns false.
  4. $wouldBlock is set to 1, which tells you the failure was caused by another lock.

Example 3 — A Safe Visit Counter

Read, add one and write back inside a lock so that no count is lost.

PHP
<?php
function bump(string $file): int {
    $fp = fopen($file, 'c+');
    flock($fp, LOCK_EX);

    $n = (int) stream_get_contents($fp) + 1;

    ftruncate($fp, 0);
    rewind($fp);
    fwrite($fp, (string) $n);
    fflush($fp);

    flock($fp, LOCK_UN);
    fclose($fp);
    return $n;
}

$file = __DIR__ . '/counter.txt';
file_put_contents($file, '0');

for ($i = 0; $i < 3; $i++) {
    echo 'Counter: ', bump($file), PHP_EOL;
}

unlink($file);

How It Works

  1. The c+ mode opens the file for reading and writing without clearing it.
  2. The exclusive lock makes sure no other script changes the number while we work.
  3. The current number is read, increased by 1, and written back from the start of the file.
  4. The lock is released at the end, so the next call can continue.

Frequently Asked Questions

It places or removes a lock on an open file so that scripts do not write to the same file at the same time.
LOCK_SH is a shared lock for reading, so many scripts can hold it together. LOCK_EX is an exclusive lock for writing, and only one script can hold it.
Combine the operation with LOCK_NB, for example LOCK_EX | LOCK_NB. flock() then returns false at once if the file is locked.
It is good practice. The lock is also released when you call fclose() or when the script ends, but unlocking early lets other scripts continue sooner.
Only if they also use flock(). It is advisory, so programs that ignore locking can still change the file.

Next: fnmatch()

fnmatch() checks whether a file name matches a shell style pattern, such as *.txt.

fnmatch() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful