simple code check please? PHP code to append xml file
Posted: Mon Sep 27, 2004 5:28 pm
I want to append an xml node to an existing xml file. I'm sending xml data from flash to a php file with the following code:
To keep it easy, I'm simply replacing the xml closing tag with the new node+closing tag (line 11). But my php code doesn't seem to work!
Seems like an php-xml append script should be fairly easy/common, but I haven't found anything online except for the simple script on Kirupa.com, which won't work if two instances of the xml file are open at the same time. (one of the stories gets deleted in that case).
Thanks for your help!
Code: Select all
<?php
//variables
$raw_xml = file_get_contents('php://input');
$stories = file_get_contents("Stories.xml");
//ignore the document type
$raw_xml = preg_replace("/\<\?xml.*?\>/", '', $raw_xml);
$raw_xml = trim($raw_xml);
//add new story
$stories = preg_replace("</stories>", "$raw_xml </stories>", $stories);
//open the file for writing
$fp = fopen("Stories.xml", "wb");
//write to file
fwrite($fp, $stories);
fclose($fp);
//send to flash
print $stories;
?>Seems like an php-xml append script should be fairly easy/common, but I haven't found anything online except for the simple script on Kirupa.com, which won't work if two instances of the xml file are open at the same time. (one of the stories gets deleted in that case).
Thanks for your help!