PHP flock () function
Definition and Usage
flock () function to lock or unlock the file.
If successful, the function returns TRUE. If it fails, it returns FALSE.
grammar
flock(file,lock,block)
参数 | 描述 |
---|---|
file | 必需。规定要锁定或释放的已打开的文件。 |
lock | 必需。规定要使用哪种锁定类型。 可能的值:
|
block | 可选。若设置为 1,则当进行锁定时阻塞其他进程。 |
Tips and Notes
Note: Use only locks in the current PHP process.If permissions allow, other processes can modify or delete a PHP-locked files.
Note: flock () is mandatory under Windows.
Tip: You can () to release the lock operation by fclose, will automatically call the script completes.
Examples
<?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);
?>
$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);
?>
Complete PHP Filesystem Reference Manual