English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Filesystem Reference Manual
The flock() function can lock or unlock a file, and returns true on success, false on failure.
bool flock ( resource $handle , int $operation [, int &$wouldblock ] )
The flock() function allows us to perform simple reader/Writer model, which can be used almost on all platforms.
Comment: These locks are only used in the current PHP process. If permissions allow, other processes can modify or delete a PHP-Locked file.
Comment: The flock() function is mandatory on Windows.
Hint: The locking operation can be released by fclose(), and it will be automatically called when the script execution is complete.
Parameters | Description |
---|---|
handle | Required. Specify the opened file to lock or unlock. |
operation | Required. Specify the type of locking to use. Possible values:
|
wouldblock | Optional. If set to 1If set, it will block other processes when locking. |
Example of using the exclusive lock LOCK_EX of fclock:
<?php $file = fopen("/PhpProject/sample.txt", "w+"); //Exclusive lock if(flock($file, LOCK_EX)) { fwrite($file, "flock function"); // Unlock lock flock($file, LOCK_UN); echo $file; } else { echo "An error occurred while locking the file!"; } fclose($file); ?>