我有一个正在进行的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>";
}
答案 0 :(得分:1)
$fp = fopen("xml/units/$unitsarray[$i].xml","wb");
通过将文件打开为“wb”,您将截断要写入的文件。尝试使用“ab”(只写,附加到文件末尾)或“ab +”(读取或写入,附加到文件末尾)。