cardio5643 Posted July 30, 2009 Share Posted July 30, 2009 This code creates and writes to file <?php $xmlFile = "../etc/someXMLfile.xml"; $xmlHandle = fopen($xmlFile, "w"); fwrite($xmlHandle, 'test'); fclose($xmlHandle); ?> This code does create, but does NOT write to file <?php $xmlFile = "../etc/someXMLfile.xml"; $xmlHandle = fopen($xmlFile, "w"); $flashXML = simplexml_load_string('<root><number>1</number></root>'); fwrite($xmlHandle, 'test'); fclose($xmlHandle); ?> This is the goal code: <?php $flashRAW = file_get_contents("php://input"); $xml = simplexml_load_string($flashRAW); $str = $xml->number->{0}; $xmlFile = "../etc/someXMLfile.xml"; $xmlHandle = fopen($xmlFile, "w"); fwrite($xmlHandle, $str); fclose($xmlHandle); ?> Now, who can tell me what I am doing wrong - because I have tried with no luck. h e l p Link to comment https://forums.phpfreaks.com/topic/168167-new-guess-i-am-just-stuck/ Share on other sites More sharing options...
MatthewJ Posted July 30, 2009 Share Posted July 30, 2009 Like Aaron suggested on the other thread, is it possible to update your php install? I get a file with Test in it for both chunks of code. Link to comment https://forums.phpfreaks.com/topic/168167-new-guess-i-am-just-stuck/#findComment-886946 Share on other sites More sharing options...
MatthewJ Posted July 30, 2009 Share Posted July 30, 2009 Also, <?php $flashRAW = file_get_contents("someXMLfile.xml"); $xml = simplexml_load_string($flashRAW); $str = $xml->number; $xmlFile = "test.xml"; $xmlHandle = fopen($xmlFile, "w"); fwrite($xmlHandle, $str); fclose($xmlHandle); ?> That code works fine for me... I changed the output file name so I wouldn't overwrite the other one as you had them both named someXMLfile.xml I also chaged this line $str = $xml->number->{0}; to $str = $xml->number; since there is only one number element in your first examples I end up with a file called test.xml with a 1 in it Link to comment https://forums.phpfreaks.com/topic/168167-new-guess-i-am-just-stuck/#findComment-886956 Share on other sites More sharing options...
roopurt18 Posted July 30, 2009 Share Posted July 30, 2009 You're going to annoy the moderators if you keep creating new topics. <?php $xmlFile = "../etc/someXMLfile.xml"; // line 1 $xmlHandle = fopen($xmlFile, "w"); // line 2 $flashXML = simplexml_load_string('<root><number>1</number></root>'); // line 3 fwrite($xmlHandle, 'test'); // line 4 fclose($xmlHandle); // line 5 ?> The reason that creates but does not write to the file is simple. Lines 1 and 2 are executed, line 3 is crashing and terminating the program. <?php $xmlFile = "../etc/someXMLfile.xml"; $xmlHandle = fopen($xmlFile, "w"); fwrite($xmlHandle, 'test'); fclose($xmlHandle); $flashXML = simplexml_load_string('<root><number>1</number></root>'); ?> Try that and you're file will be created. Now do like I said in your other-other thread and validate your XML before sending it to SimpleXML. Link to comment https://forums.phpfreaks.com/topic/168167-new-guess-i-am-just-stuck/#findComment-886957 Share on other sites More sharing options...
cardio5643 Posted July 30, 2009 Author Share Posted July 30, 2009 You're going to annoy the moderators if you keep creating new topics. <?php $xmlFile = "../etc/someXMLfile.xml"; // line 1 $xmlHandle = fopen($xmlFile, "w"); // line 2 $flashXML = simplexml_load_string('<root><number>1</number></root>'); // line 3 fwrite($xmlHandle, 'test'); // line 4 fclose($xmlHandle); // line 5 ?> The reason that creates but does not write to the file is simple. Lines 1 and 2 are executed, line 3 is crashing and terminating the program. <?php $xmlFile = "../etc/someXMLfile.xml"; $xmlHandle = fopen($xmlFile, "w"); fwrite($xmlHandle, 'test'); fclose($xmlHandle); $flashXML = simplexml_load_string('<root><number>1</number></root>'); ?> Try that and you're file will be created. Now do like I said in your other-other thread and validate your XML before sending it to SimpleXML. Obviously it is crashing, but I am not trying to write the word test to a file, i am trying to write the contents of the xml, meaning where the word test is i would have something like $xml->number, but that does not work either. so to prove that the simplexml JUST BEING IN THE FILE crashes the file, i put it in there but kept the text 'test' to write in order to debug. my xml looks validated to me - but maybe someone knows better and i do not mean to be a bother, my first thread was dying, my second didnt make sense, so this is my third. will not do again. Link to comment https://forums.phpfreaks.com/topic/168167-new-guess-i-am-just-stuck/#findComment-886963 Share on other sites More sharing options...
cardio5643 Posted July 30, 2009 Author Share Posted July 30, 2009 Also, <?php $flashRAW = file_get_contents("someXMLfile.xml"); $xml = simplexml_load_string($flashRAW); $str = $xml->number; $xmlFile = "test.xml"; $xmlHandle = fopen($xmlFile, "w"); fwrite($xmlHandle, $str); fclose($xmlHandle); ?> That code works fine for me... I changed the output file name so I wouldn't overwrite the other one as you had them both named someXMLfile.xml I also chaged this line $str = $xml->number->{0}; to $str = $xml->number; since there is only one number element in your first examples I end up with a file called test.xml with a 1 in it but i am not getting my contents from a file, i am getting them from an asynchronous javascript routine. Link to comment https://forums.phpfreaks.com/topic/168167-new-guess-i-am-just-stuck/#findComment-886969 Share on other sites More sharing options...
MatthewJ Posted July 30, 2009 Share Posted July 30, 2009 simplexml_load_string(); returns an object... is is not the xml string anymore, but an object representing it. You would need to regenerate the xml placing into it the contents from the simplexml object. My question would be, why are you loading it into a variable with file_get_contents() only to write it right back into the same format? Link to comment https://forums.phpfreaks.com/topic/168167-new-guess-i-am-just-stuck/#findComment-887004 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.