Home > Article > Backend Development > How to use php flock function
php flock() function is used to lock or release files, the syntax is flock(file,lock,block). If successful, the function returns TRUE. On failure, returns FALSE.
How to use php flock() function?
php flock() function locks or releases files.
Note: The file operated by flock() must be an open file pointer.
Syntax:
flock(file,lock,block)
Parameters:
file: required. Specifies an open file to be locked or released.
lock: required. Specifies which lock type to use.
block: optional. If set to 1 or true, blocks other processes while locking.
Return value: If successful, this function returns TRUE. On failure, returns FALSE.
Note: These locks are only used within the current PHP process. If permissions allow, other processes can modify or delete a PHP-locked file.
php flock() function example
<?php $file = fopen("test.txt", "w+"); // 排他锁 if (flock($file, LOCK_EX)) { fwrite($file, "Write something"); // 释放锁 flock($file, LOCK_UN); echo "success"; } else { echo "Error locking file!"; } fclose($file); ?>
The above is the detailed content of How to use php flock function. For more information, please follow other related articles on the PHP Chinese website!