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
| Function | Purpose |
|---|---|
flock() | Applies advisory shared or exclusive locking to a file stream. |
file_put_contents() with LOCK_EX | LOCK_EX in file_put_contents() locks while writing; flock() lets you coordinate a larger read-modify-write sequence. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: Locks are advisory and generally work only when cooperating processes follow the same locking convention; always release locks | Locks 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.