- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用:
file_put_contents("peopleList.txt", $person, FILE_APPEND | LOCK_EX);
写入文件末尾并确保没有其他人(或脚本)同时写入同一文件。
PHP manual表示如果不成功,它将返回一个假值。
如果它不能获得文件的锁,它会失败,还是继续尝试直到它可以?如果在无法获得锁的情况下确实失败,确保数据写入的最佳方法是什么?
也许在 while 循环中循环函数直到它不返回 false(畏缩)或者只是向用户(网站访问者)提供某种 GUI 以请求他们重试?
最佳答案
其实我之前的回答有点过时了。 flock()
block 直到 requested lock is acquired :
PHP supports a portable way of locking complete files in an advisory way (which means all accessing programs have to use the same way of locking or it will not work). By default, this function will block until the requested lock is acquired; this may be controlled (on non-Windows platforms) with the LOCK_NB option documented below.
既然 file_get_contents()
使用了它,我假设它是一样的。也就是说,请注意它因操作系统而异。
更重要的是,您不需要在您描述的场景中锁定文件,原因是 N.B.已经解释过了。除非您使用的是 CLI SAPI,否则我想不出您应该担心文件锁定的常见情况。
关于php - 如何将 file_put_contents() 与 FILE_APPEND 一起使用 | LOCK_EX 安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9009058/
为什么我无法读取使用LOCK_EX锁定的文件?我仍然可以写入该文件。 我想知道,如果一个进程锁定一个文件(使用 LOCK_SH 或 LOCK_EX),而另一个进程尝试读取该文件或写入该文件,会发生什么
为什么我无法读取使用LOCK_EX锁定的文件?我仍然可以写入该文件。 我想知道,如果一个进程锁定一个文件(使用 LOCK_SH 或 LOCK_EX),而另一个进程尝试读取该文件或写入该文件,会发生什么
一个简单的问题。尝试读取使用 LOCK_EX 的 file_put_contents 锁定的文件的脚本是否会等到文件解锁或读取失败并继续? 最佳答案 简单的答案是它们不会阻塞但会失败。 Groovep
我在 flock manual 找到了以下描述: By default, this function will block until the requested lock is acquired 在
考虑这样一种情况,其中两个进程并发尝试使用 flock(fd, LOCK_EX|LOCK_NB) 对某个文件放置独占锁。 如前所述,尝试是非阻塞,因此这两个进程之一应该会因 EWOULDBLOCK 而
我正在使用: file_put_contents("peopleList.txt", $person, FILE_APPEND | LOCK_EX); 写入文件末尾并确保没有其他人(或脚本)同时写入同
我是一名优秀的程序员,十分优秀!