PHP implements file locking and unlocking methods

墨辰丷
Release: 2023-03-27 06:16:02
Original
1900 people have browsed it

This article mainly introduces the method of PHP to implement file locking and unlocking operations. It analyzes the functions, implementation methods and related precautions of PHP for file locking and unlocking operations in the form of examples. Friends in need You can refer to the following

In projects, logs are generally used, such as database query logs, access logs, and external interface request return parameter logs. When processing logs, the simple writing method is as follows

$file = 'log.txt';
$fp = fopen($file, 'a+');
if(!is_writable($file)){
  die("The $file is not writable!");
}
fwrite($fp, 'here');
fclose($fp);
Copy after login

But this way of writing is flawed. A website is not only visited by one user at the same time. When multiple users visit it at the same time, problems will arise, that is, When multiple processes use the same resource, the previous process will start writing halfway before the subsequent process starts writing, so the final log generated will be messed up. In this case, locks are used. During the file locking period, other processes will not modify the file. They can only operate when the file is unlocked. The writing method is as follows

$file = 'log.txt';
$fp = fopen($file, 'a+');
if(!is_writable($file)){
  exit("The $file is not writable!");
}
flock($fp, LOCK_EX);// 加锁
fwrite($fp, 'here');
flock($fp, LOCK_UN);// 解锁
fclose($fp);
Copy after login

If you want to test the example where other processes cannot operate the file during the file locking period, you can use the demo

## given below #log.php

$file = 'log.txt';
$fp = fopen($file, 'a+');
if(!is_writable($file)){
  exit("The $file is not writable!");
}
flock($fp, LOCK_EX);
fwrite($fp, 'here');
sleep(10);
flock($fp, LOCK_UN);
fclose($fp);
Copy after login

test.php

$file = 'lock.txt';
$fp = fopen($file, 'a');
fwrite($fp, 'good'); // 在sleep期间写不进去
fclose($fp);
// 或是直接使用下面的这个例子,发现在sleep期间打印是个空值
//var_dump(file_get_contents($file));
Copy after login

When testing, run first log.php, and then run test.php, you will find that test.php cannot be executed during sleep.

Related recommendations:

php file lockSolution to high concurrency

Detailed explanation of PHP file lock concurrent operations

Example analysis of PHP file lock and process lock

The above is the detailed content of PHP implements file locking and unlocking methods. 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