file_put_contents上的互斥标志?

时间:2009-08-22 03:37:42

标签: php file locking flags

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标志是否相互排斥?如果是,为什么他们在示例中使用它?这是一个不良文档的案例吗?

感谢您的投入!

2 个答案:

答案 0 :(得分:4)

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

(需要一些时间才能反映在手册的在线版本中,但在其来源中已得到纠正)

答案 1 :(得分:3)

那只是糟糕的文档。 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);
?>

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