Home  >  Article  >  Backend Development  >  How to use php flock function

How to use php flock function

青灯夜游
青灯夜游Original
2019-05-28 15:35:582620browse

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

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn