PHP检查文件是否与flock()锁定? [英] PHP check if file locked with flock()?

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

问题描述

如果文件存在, fopen()会失败,但是当前被锁定为 LOCK_EX



或者我必须打开它,然后尝试和设置一个锁,以确定是否已经存在?



我也读过 flock() will;


暂停[脚本],直到你获得无限期的锁定时间,或者直到你的脚本超时为止。

http://www.php.net/manual/en/function.flock.php#95257 <如果是这样的话,这个暂停可以绕过;






  if(!flock($ f,LOCK_SH | LOCK_NB)){
//文件锁定,执行其他操作
}

解决方案

flock()实际上阻​​止你读/写文件,它只允许你沟通我deas锁定到其他脚本。您可以使用您发布的代码段来检测文件是否存在锁定。


Will fopen() fail if a file exists, but is currently locked with LOCK_EX?

Or do I have to open it, and then try and set a lock, in order to determine if one already exists?

I've also read that flock() will;

pause [the script] untill you get the lock for indefinite amount of time or till your script times out

http://www.php.net/manual/en/function.flock.php#95257

If so, is it true this 'pause' can be by-passed with;

if (!flock($f, LOCK_SH | LOCK_NB)) {
    // file locked, do something else
}

解决方案

flock() doesn't actually prevent you from reading/writing to a file, it only allows you to "communicate" the ideas of locking to other scripts. You can detect if there is a lock on a file using the snippet you posted.

这篇关于PHP检查文件是否与flock()锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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