【发布时间】:2013-03-09 03:25:02
【问题描述】:
PHP 允许我们在执行fopen 时使用x 标志:
创建和打开仅供写作;将文件指针放在 文件的开头。
如果文件已经存在,则 fopen() 调用 将通过返回 FALSE 并生成 level 错误而失败 E_WARNING。
如果文件不存在,请尝试创建它。这是 相当于为底层指定 O_EXCL|O_CREAT 标志 open(2) 系统调用。
这是否意味着无论我们有多少并发 fopen 请求(来自不同用户),保证文件只会被创建一次并且永远不会被覆盖?
if ($handle = fopen("part006", "x+b")) {
do_some_processing();
echo "You managed to process.";
/*
can we guarantee that only 1 user (http request)
will ever process the function and see the
message "you managed to process" ?
*/
} else {
echo "You failed to process.";
}
【问题讨论】:
标签: php web-applications locking mutex fopen