Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 附加在xml文件末尾的标记_Php_Simplexml - Fatal编程技术网

Php 附加在xml文件末尾的标记

Php 附加在xml文件末尾的标记,php,simplexml,Php,Simplexml,我有一个xml文件,格式如下- <?xml version="1.0"?> <db> <count></count> <uid></uid> <score></score> </db> 然后像这样关上它- $result= new SimpleXMLElement($file, null, true); file_put_contents($file, $data->asXML(

我有一个xml文件,格式如下-

<?xml version="1.0"?>
<db>
<count></count>
<uid></uid>
<score></score>
</db>
然后像这样关上它-

$result= new SimpleXMLElement($file, null, true);
file_put_contents($file, $data->asXML()); 
当我在本地服务器上运行它时,一切都很正常,但是当我在服务器上在线上传它时,它可以正常工作一段时间,一段时间后,它会附加到xml文件的末尾

<?xml version="1.0"?>
<db>
<count></count>
<uid></uid>
<score></score>
</db>
/score></db>

虽然没有明确说明,但似乎
file\u put\u contents
的默认行为不是锁定正在写入的文件。这可能意味着两个PHP进程都试图同时覆盖该文件,这可能是导致损坏的原因

为了消除这种可能性,您可以将
LOCK\u EX
标志作为第三个参数传递,即
file\u put\u contents($file,$data->asXML(),LOCK\u EX)


(我不知道如何验证这个假设,因为这可能是您遇到的一种罕见的竞争情况…

我们需要查看在保存之前添加元素的代码。我认为您使用的stru_replace可能会导致问题,使我的ans包含更新xml文件的代码。@PrashantBaid不要惊慌,您键入时,我正在发布答案;)在将LOCK_EX添加为参数后,没有出现该错误。我想这就是我犯错误的原因。非常感谢你救了我的命:)
$data= new SimpleXMLElement($file, null, true);
$winner=intval($data->score[$winid]);
$loser=intval($data->score[$loseid]);
$exp_winner=expected($loser,$winner);
$new_win=win($winner,$exp_winner);
$exp_loser=expected($winner,$loser);
$new_lose=loss($loser,$exp_loser);
$data->score[$winid]=$new_win;
$data->score[$loseid]==$new_lose;
file_put_contents($file, $data->asXML());