4

file_put_contents () 文档中,它说明了以下内容:

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

感谢您的输入!

4

2 回答 2

4

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

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

于 2009-09-07T06:53:44.720 回答
3

那只是糟糕的文档。该手册明确指出

FILE_APPEND:如果文件 filename 已经存在,则将数据附加到文件而不是覆盖它。与 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);
?>

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

于 2009-08-22T03:40:35.653 回答