Lock and release a file:
<?php
$file = fopen("test.txt","w+");
// exclusive lock
if (flock($file,LOCK_EX)) {
fwrite($file,"Add some text to the file.");
fflush($file);
// release lock
flock($file,LOCK_UN);
} else {
echo "Error locking file!";
}
fclose($file);
?>
The flock() function locks and releases a file.
flock(
file,
lock,
block)
Parameter | Description |
---|---|
file | Required. Specifies an open file to lock or release |
lock | Required. Specifies what kind of lock to use. Possible values:
|
block | Optional. Set to 1 to block other processes while locking |
Return Value: | TRUE on success, FALSE on failure |
---|---|
PHP Version: | 4.0+ |
PHP Changelog: | PHP 5.5: Added support for the block parameter on Windows PHP 5.3: Removed automatic unlocking on fclose(). Unlocking must now be done manually |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!