Reputation: 859
Here's my code:
public function write($txt){
$randID = rand();
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->load($this->fileName);
$b = $doc->createElement( "input");
$id = $doc->createElement( "id",$randID);
$b->appendChild($id);
$text = $doc->createElement( "text",$txt);
$b->appendChild( $text );
$doc->appendChild($b);
$doc->save($this->fileName);
}
Why does this code always overwrite the old file? Why doesn't it append to the end of the existing file? I'm new to XML with PHP :)
Upvotes: 0
Views: 130
Reputation: 9929
I think it's because it just loads the file in memory, than adds the xml you want and than saves it to disk.
Because you use the same filename while saving, it overwrites the original file with a new file containing all XML, including the new elements and data. Makes sense to me.
Upvotes: 2