加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – file_put_contents上的互斥标志?

发布时间:2020-12-13 21:35:05 所属栏目:PHP教程 来源:网络整理
导读:在 file_put_contents()文档中,它说明如下: FILE_APPEND: Mutually exclusive with LOCK_EX since appends are atomic and thus there is no reason to lock. LOCK_EX: Mutually exclusive with FILE_APPEND. 然而,下面的几行代码我看到以下代码: ?php$f
在 file_put_contents()文档中,它说明如下:

FILE_APPEND:

Mutually exclusive with LOCK_EX since
appends are atomic and thus there is
no reason to lock.

LOCK_EX:

Mutually exclusive with FILE_APPEND.

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

<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smithn";
// 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标志是否相互排斥?如果是,为什么他们在示例中使用它?这是一个糟糕的文件案例吗?

感谢您的输入!

解决方法

那只是糟糕的文档. manual clearly states:

FILE_APPEND : If file filename
already exists,append the data to the
file instead of overwriting it.
Mutually exclusive with LOCK_EX since
appends are atomic and thus there is
no reason to lock.

LOCK_EX : Acquire an exclusive lock
on the file while proceeding to the
writing. Mutually exclusive with
FILE_APPEND.

你说的例子是:

<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smithn";
// Write the contents to the file,FILE_APPEND | LOCK_EX);
?>

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读