本文介绍了测试文件是否被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PHP中,如何测试文件是否已经被 flock
?例如,如果另一个正在运行的脚本调用了以下内容:
In PHP, how can I test if a file has already been locked with flock
? For example, if another running script has called the following:
$fp = fopen('thefile.txt', 'w');
flock($fp, LOCK_EX);
推荐答案
if (!flock($fp, LOCK_EX|LOCK_NB, $wouldblock)) {
if ($wouldblock) {
// another process holds the lock
}
else {
// couldn't lock for another reason, e.g. no such file
}
}
else {
// lock obtained
}
如文档中所述,请使用LOCK_NB
进行进行非阻塞性尝试以获取该锁,并在失败时检查$wouldblock
参数以查看是否有其他东西持有该锁.
As described in the docs, use LOCK_NB
to make a non-blocking attempt to obtain the lock, and on failure check the $wouldblock
argument to see if something else holds the lock.
这篇关于测试文件是否被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!