In PHP, flock is a function, which means locking or releasing a file; the function of this function is to lock the file when operating the file. It is only available when the lock is cancelled. If successful, the function returns true. If it fails, the function returns false, and the syntax is "flock(file,lock,block)".
The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer
What does flock mean in php
## The #flock() function locks or releases a file. The function of flock in php is to lock the file when operating the file, and it is only available when the lock is unlocked.
If successful, this function returns TRUE. On failure, returns FALSE.
Syntax
flock(file,lock,block)
Copy after login
Parameter Description
- file Required. Specifies an open file to be locked or released.
- lock Required. Specifies which lock type to use. Possible values:
LOCK_SH - Shared lock (reading program). Allow other processes to access the file.
LOCK_EX - Exclusive lock (writing program). Prevent other processes from accessing the file.
LOCK_UN - Release a shared lock or exclusive lock
LOCK_NB - Avoid blocking other processes in case of a lock.
- block Optional. If set to 1, blocks other processes while locking.
These locks are only used within the current PHP process. If permissions allow, other processes can modify or delete a PHP-locked file.
Note: flock() is mandatory under Windows.
Tip: You can use fclose() to release the lock operation, which will be automatically called when the script execution is completed.
Examples are as follows:
<?php
$file = fopen("test.txt","w+");
// exclusive lock
if (flock($file,LOCK_EX))
{
fwrite($file,"Write something");
// release lock
flock($file,LOCK_UN);
}
else
{
echo "Error locking file!";
}
fclose($file);
?>
Copy after login
Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of What does flock mean in php. For more information, please follow other related articles on the PHP Chinese website!