0

我有一个正在进行的 xml 文件,当我调用一个 php 函数来添加一个新子项时,它会遍历一个字符串数组并查询一个 db 以将一个新子项添加到文档中并将其保存为数组中的当前字符串。但是,它不是附加的,而是覆盖所有内容。我需要先加载文件吗?并检查它是否存在?

function createUnitsXML($units,$wcccanumber,$mysqli) {

// Delete whitespaces and create an array of units assigned to call
$unit = preg_replace('/\s+/', '', $units);
$unitsarray = explode(",",$unit);

    for ($i = 0; $i < count($unitsarray); $i++) {

        $xml = new SimpleXMLElement('<xml/>');

        $query = "SELECT * FROM calls WHERE wcccanumber = '$wcccanumber'";
        $result = $mysqli->query($query);

        while($row = mysqli_fetch_assoc($result)) {
             $draw = $xml->addChild('call');
             $draw->addChild('wcccanumber',$row['wcccanumber']);
             $draw->addChild('currentcall',$row['call']);
             $draw->addChild('county',$row['county']);
             $draw->addChild('id',$row['id']);
             $draw->addChild('location',$row['location']);
             $draw->addChild('callcreated',$row['callcreated']);
             $draw->addChild('station',$row['station']);
             $draw->addChild('units',$row['units']);
             $draw->addChild('calltype',$row['calltype']);
             $draw->addChild('lat',$row['lat']);
             $draw->addChild('lng',$row['lng']);
             $draw->addChild('inputtime',$row['inputtime']);
        }
        $fp = fopen("xml/units/$unitsarray[$i].xml","wb");
        fwrite($fp,$xml->asXML());
        fclose($fp);
    }
    echo "--- Created units XML document for call: $wcccanumber";
    echo "</br>";
}
4

1 回答 1

1
$fp = fopen("xml/units/$unitsarray[$i].xml","wb");

通过将文件打开为“wb”,您正在截断要写入的文件。尝试使用“ab”(只写,附加到文件末尾)或“ab+”(读取或写入,附加到文件末尾)。

于 2012-12-18T19:06:25.777 回答