【问题标题】:Mutually exclusive flags on file_put_contents?file_put_contents 上的互斥标志?
【发布时间】:2010-11-21 20:26:28
【问题描述】:

file_put_contents() 文档中,它说如下:

FILE_APPEND

自 LOCK_EX 互斥 附加是原子的,因此有 没有理由锁定。

LOCK_EX

与 FILE_APPEND 互斥。

然而,下面几行我看到了以下代码:

<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>

那么,FILE_APPEND 和 LOCK_EX 标志是否互斥?如果是,为什么他们在示例中使用它?这是不是文档错误的案例?

感谢您的意见!

【问题讨论】:

  • 不仔细看,听起来就像编写示例的人不熟悉文档。或者,可能在编写示例后 API 发生了变化,并且没有人更新示例。
  • 谢谢马克,+1 我开始怀疑是否有任何我不知道的晦涩原因。
  • 这已为bug #52767 正确修复。标志不是互斥的。

标签: php file locking flags


【解决方案1】:

@karim79 said 一样,这是手册中的错误:请参阅bug #49329,我在看到此问题/答案后报告,并已在几分钟前更正/关闭。

(这需要一些时间才能反映在手册的在线版本中,但已在其来源中进行了更正)

【讨论】:

    【解决方案2】:

    这只是糟糕的文档。 manual clearly states

    FILE_APPEND : 如果文件文件名 已经存在,将数据追加到 文件而不是覆盖它。 与 LOCK_EX 互斥 附加是原子的,因此有 没有理由锁定。

    LOCK_EX : 获取排他锁 在文件上,同时进行 写作。与互斥 FILE_APPEND。

    还有你说的例子:

    <?php
    $file = 'people.txt';
    // The new person to add to the file
    $person = "John Smith\n";
    // Write the contents to the file, 
    // using the FILE_APPEND flag to append the content to the end of the file
    // and the LOCK_EX flag to prevent anyone else writing to the file at the same time
    file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
    ?>
    

    看起来编写示例的人误解了“互斥”的含义,会产生一些秘密的、无证的行为。

    【讨论】:

    • 什么意思? '互斥'
    猜你喜欢
    • 2011-01-06
    • 2018-05-17
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多