2009-08-22 99 views
3

file_put_contents()文檔,它說以下內容: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

沒有過多密切關注,聽起來像是誰寫的例子不熟悉文檔。或者,也許自該示例寫入後API已更改,並且沒有人更新該示例。 – 2009-08-22 03:43:43

+0

感謝馬克,+1我開始懷疑是否有任何我不知道的不明原因。 – 2009-08-22 03:46:52

+0

這已正確修復爲[bug#52767](http://bugs.php.net/bug.php?id=52767)。標誌*不相互排斥。 – Artefacto 2010-09-08 00:49:14

回答

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); 
?> 

看起來誰編碼的例子誤解的「互斥」的含義的人,產生一些祕密,無證bahaviour。

4

@karim79 said一樣,這是手冊中的錯誤:請參閱bug #49329,我在看到此問題/答案後報告了這種情況,並且在幾分鐘前已被更正/關閉。

(這將需要一些時間來反映在手冊的在線版本,但在其來源AHS被糾正)