php flock() function is used for lightweight advisory file locking.
php flock() function syntax
Function: lock or release the file.
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.
Note: The file operated by flock() must be an open file pointer.
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!"; } ?>
This article is an introduction to the PHP flock function. I hope it will be helpful to friends in need!
The above is the detailed content of How to use flock function. For more information, please follow other related articles on the PHP Chinese website!