Home > Backend Development > PHP Tutorial > How to use flock function

How to use flock function

藏色散人
Release: 2023-04-05 09:50:01
Original
2738 people have browsed it

php flock() function is used for lightweight advisory file locking.

How to use flock function

php flock() function syntax

Function: lock or release the file.

Syntax:

flock(file,lock,block)
Copy after login

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!";
}
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template