PHP check if file locked with flock()?

旧城冷巷雨未停 提交于 2019-11-30 00:57:47

问题


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
}

回答1:


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.



来源:https://stackoverflow.com/questions/3149324/php-check-if-file-locked-with-flock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!