I’m having a small problem here. I am running a localhost Apache PHP server. What I am trying to do is load an xml file, modify it, and send it back to the server with PHP.
I am able to load the xml file and modify it but when I save back to the server the xml file is empty or null.
Here is the as code:
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest(‘test.xml’));
function processXML(event:Event):void {
myXML=new XML(myLoader.data);
}
var xmlcontents:String=myXML;
var foldername:String=“testfolder”;
var filename:String=“test.xml”;
var dataPass:URLVariables = new URLVariables();
var urlLoader:URLLoader = new URLLoader();
var previewRequest:URLRequest=new URLRequest(“http://localhost/jd/saving-xml.php”);
previewRequest.method=URLRequestMethod.POST;
dataPass.filename=filename;
dataPass.xmlcontents=xmlcontents;
dataPass.foldername=foldername;
previewRequest.data=dataPass;
urlLoader.load(previewRequest);
When I do this in the as code it works fine:
var myXML:XML=
<people>
<item>
<name>George</name>
</item>
<item>
<name>Bill</name>
</item>
<item>
<name>Henry</name>
</item>
</people>;
It’s only when I load the xml document externally I am getting a blank xml document when I save the xml back to the server.
The PHP code )I think this part is fine):
<?php
// POST variable
$fileName = $_POST[“filename”];
// POST variable
$xmlContents = $_POST["xmlcontents"];
// backslashes from xml string (skip this for plain text)
$lastBackslashPos = strpos ($xmlContents, "\\");
while($lastBackslashPos >0){
$xmlContents = substr($xmlContents,0,$lastBackslashPos)
.substr($xmlContents,$lastBackslashPos+1,strlen($xmlContents));
$lastBackslashPos = strpos ($xmlContents, "\\");
}
// POST foldername
$foldername = $_POST["foldername"];
// test if the folder name is existing in the server (skip this if you want to test in server path)
if(!is_dir($foldername . "/")) {
mkdir($foldername . "/", 0755);
}
// write xml data to file on server relatively to server path of the this PHP file
$fh = fopen($foldername . "/" . $fileName, "w");
fwrite($fh, $xmlContents);
fclose($fh);
?>
Thanks in advance.