Home > Backend Development > PHP Problem > Does php know about file locks? What is the use?

Does php know about file locks? What is the use?

青灯夜游
Release: 2023-03-11 22:38:02
Original
3698 people have browsed it

In the previous article, we introduced the method of using PHP to obtain files in the specified directory (excluding subdirectories). If necessary, please see "How does php know which files are in the specified directory" . This time we will introduce file lock to you, look at the function of file lock, and how to add file lock and release file lock.

In the previous article "php file operation: How to clear the file and re-add data" we introduced the method of writing data to the file. In the article, we are a single user at the same time. Manipulate the file so there is no problem. However, if in a network environment, multiple client users access the same file on the server at the same time, then the program will have errors and the file may be damaged.

For example:

  • A user is writing data to the file (the data is not finished yet). At this time, another user is also writing data to this file. Writing data into the file will cause confusion in data writing;

  • Or, if the user has not finished writing the data, other users will obtain the contents of the file. Then the data obtained by the user is incomplete and incomplete data will be obtained.

In order to solve this problem, PHP provides the flock() function - a file lock mechanism that can lock or release files.

When writing to a file using PHP, the file is temporarily locked; only after the lock is released, other users can operate the file. This avoids data corruption when concurrently accessing the same file.

Let’s take a look at the following example:

<?php
header("Content-Type: text/html;charset=utf-8");    //设置字符编码
$file = "test.txt";
$handle = fopen($file,&#39;w+&#39;); //打开文件
if(flock($handle, LOCK_EX|LOCK_NB)){  //给文件上锁
    fwrite($handle, &#39;现在是:&#39;.date(&#39;Y-m-d H:i:s&#39;, time()));  //写入数据
    flock($handle, LOCK_UN);  //释放文件锁
}else{
    echo "string";
}
fclose($handle);//关闭文件
readfile($file);//读取文件内容
?>
Copy after login

Analyze the above code:

First use the fopen() function to open a file. Then use the flock($handle, LOCK_EX|LOCK_NB) statement to add a "LOCK_EX" exclusive lock to the file, and set "LOCK_NB" to avoid blocking other processes when locked, and directly return the content in else. Then you can use the fwrite() function to perform a writing operation and write the current time.

After the file is operated, you need to use the fclose() function to close the file; finally, use the readfile() function to read the file content. Therefore, the output result is:

Does php know about file locks? What is the use?

# Let’s take a brief look at the flock() function.

flock($file,$lock,$block)The function accepts 2 required parameters $file (open file that needs to be locked/unlocked) and $lock (lock type) and an omitted parameter $block (when the value is set to 1 or true, other processes can be blocked when the file is locked).

The $lock parameter is used to set the file lock type. You can set one or more of the following values ​​(multiple values ​​need to be separated by |):

  • LOCK_SH - Shared lock (reading program). Allow other processes to access the file.

  • LOCK_EX - Exclusive lock (program written). Prevent other processes from accessing the file.

  • LOCK_UN - Release a shared or exclusive lock

  • LOCK_NB - Lock situation to avoid blocking other processes.

Okay, that’s all. If you want to know anything else, you can click this. → →Basic operation of PHP files

## Recommended: PHP interview questions summary (collection)》《php Video tutorial

The above is the detailed content of Does php know about file locks? What is the use?. 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