测试文件是否被锁定 [英] Test if file is locked

查看:117
本文介绍了测试文件是否被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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.

这篇关于测试文件是否被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆