Content · Programming · PHP

PHP flock() Function: Usage and Examples

Learn how PHP flock works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Applies advisory shared or exclusive locking to a file stream.

What Is flock() in PHP?

Applies advisory shared or exclusive locking to a file stream.

Its syntax is:

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

Parameters:

  • $stream — Accepted as resource; required argument.
  • $operation — Accepted as int; required argument.
  • $would_block — Accepted as int &; optional default null.

Return type: bool. Locks are advisory and generally work only when cooperating processes follow the same locking convention; always release locks.

Availability: Requires a supported local filesystem; network filesystems differ.

Example 1: Basic Usage

<?php

$stream = tmpfile(); var_dump(flock($stream, LOCK_EX)); flock($stream, LOCK_UN); fclose($stream);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Locks are advisory and generally work only when cooperating processes follow the same locking convention; always release locks.

Example 2: A Different Input

<?php

$stream = tmpfile(); var_dump(flock($stream, LOCK_SH)); flock($stream, LOCK_UN); fclose($stream);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Locks are advisory and generally work only when cooperating processes follow the same locking convention; always release locks.

Practical Example: Applying It in a Program

<?php

$stream = tmpfile(); if (flock($stream, LOCK_EX)) { fwrite($stream, "serialized update"); fflush($stream); flock($stream, LOCK_UN); echo "Update completed"; } fclose($stream);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Locks are advisory and generally work only when cooperating processes follow the same locking convention; always release locks.

Important Behavior and Edge Cases

  • Key behavior: Locks are advisory and generally work only when cooperating processes follow the same locking convention; always release locks.
  • Environment: Requires a supported local filesystem; network filesystems differ.

Comparison With Related Functions

FunctionPurpose
flock()Applies advisory shared or exclusive locking to a file stream.
file_put_contents() with LOCK_EXLOCK_EX in file_put_contents() locks while writing; flock() lets you coordinate a larger read-modify-write sequence.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: Locks are advisory and generally work only when cooperating processes follow the same locking convention; always release locksLocks are advisory and generally work only when cooperating processes follow the same locking convention; always release locks.

Key Takeaways

  • Applies advisory shared or exclusive locking to a file stream.
  • Locks are advisory and generally work only when cooperating processes follow the same locking convention; always release locks.
  • Requires a supported local filesystem; network filesystems differ.

Summary: Locks are advisory and generally work only when cooperating processes follow the same locking convention; always release locks.

Share this resourceSend this page to someone who may find it useful.