【问题标题】:Unlocking file in PHP returns true when it shouldn't?在 PHP 中解锁文件在不应该返回 true 时返回?
【发布时间】:2022-01-16 22:30:30
【问题描述】:

在 PHP 中,我使用这样的群创建了一个咨询锁:

$fileHandle = fopen($filePath, 'c');
flock($fileHandle, LOCK_EX | LOCK_NB);

在另一个进程中运行相同的代码随后会失败,因为锁是独占的。但是第二个进程能够运行的:

$fileHandle = fopen($filePath, 'c');
flock($fileHandle, LOCK_UN); // returns true

但文件仍处于锁定状态,这已通过运行第三个进程确认。那么为什么解锁请求返回true呢?

【问题讨论】:

    标签: php fopen


    【解决方案1】:

    虽然PHP Manual page about flock() 在这方面看起来模棱两可,但我建议返回true 是因为函数成功执行,而不是因为任何文件被成功锁定或解锁。

    还请注意,PHP Manual page 显示了很多关于此功能的警告,我什至建议挖掘一个替代例程来锁定对文件的访问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      相关资源
      最近更新 更多