1

我有一个 PHP 脚本,它打开一个 XML 文档,获取 DOM 并修改它,然后覆盖原始文档。
据我了解,PHP 调用是异步进行的,因此多个用户可以同时访问文档,然后保存到该文档,第二个保存覆盖第一个。
我需要这不是这种情况。我不能使用flock(),因为这只适用于当前流程,那么我该如何实现呢?

4

2 回答 2

3

Actually, flock() doesn't only apply to the current process:

http://php.net/manual/en/function.flock.php

It does use the local filesystem, though, which may become an issue if you're using load-balanced webservers. Also, in order for flock to work you have to make sure that any other processes which may be "competing" for the file are also using flock, otherwise the processes will "step" on each other.

于 2012-06-28T02:38:16.317 回答
-1

Lock the file.

Create an empty file (e.g. xml.lock) to indicate that the xml is being opened by other process. Remove the empty file after finish modifying the xml.

于 2012-06-28T02:14:57.810 回答