使用flock()检查文件是否已被锁定? [英] Check if a file is already locked using flock()?
本文介绍了使用flock()检查文件是否已被锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个要写入的文件,但我需要先将其锁定(使用flock()
),以防止任何其他脚本写入该文件.
所以我有:
I have a file I'm writing to, but I need to lock it first (using flock()
), to prevent any other script from writing to it.
So I have:
$file=fopen($file_p);
if (flock($file, LOCK_EX)) {//lock was successful
fwrite($file,$write_contents);
}
但是我需要检查它是否已经被锁定,以防止其他脚本写入它.
But I need to check if it's already locked, to prevent other scripts from writing to it.
我该怎么做?
推荐答案
我将检查是否无法获得文件锁,如下所示:
I would check to see if I couldn't obtain a lock on the file, like this:
if (!flock($file, LOCK_EX)) {
throw new Exception(sprintf('Unable to obtain lock on file: %s', $file));
}
fwrite($file, $write_contents);
这篇关于使用flock()检查文件是否已被锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文