Page 1 of 1

Write XML code in PHP

Posted: Tue Mar 11, 2008 6:30 am
by Largo65
Hi, simple question: How do I create a PHP variable with this data: <?xml version="1.0"?><Tracks></Tracks>
so that in the "Overwrite file with new data" in my script, PHP will write this XML-data variable instead of the $raw_xml variable in my XML file?
One of the problems I ran into when trying this myself was that the PHP will not let my use the "" when I create a variable, but I need these quote symbols for the XML ... :!:
Can any one help me? :)

Code: Select all

<?php 
//This PHP script writes data that is sent from flash, into an XML file:------------------------------------------------
$filename = "xmldata/trackingData.xml"; 
$raw_xml = file_get_contents("php://input"); 
 
print $raw_xml; 
 
//Check File Size. The size returned is in BYTES 
$size = filesize($filename); 
 
//If XML file is larger than 50KB's then make a copy. 
if($size > 51200){ 
     
    //Copy file with new name containing the day it was copied 
    $today = date("Ymd"); 
    $newFile = "xmldata/trackingData".$today.".xml"; 
    copy($filename, $newFile); 
   //-------------------------------------------------------------------------!!!!!!!!!!!!!!!!!!
   //Overwrite file with new data. -----------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    $fp = fopen($filename, "w"); 
    fwrite($fp, $raw_xml); //How do I put the my XML code here?  -------------------!!!!!!!!!!
    fclose($fp); 
 
 
} else { 
    //Write normally 
    $fp = fopen($filename, "w"); 
    fwrite($fp, $raw_xml); 
    fclose($fp); 
} 
?>

Re: Write XML code in PHP

Posted: Tue Mar 11, 2008 7:17 am
by anto91
Use single quotes to avoid error messaging when writing regular quotes in a varible.

Example

Code: Select all

 
<?php
echo '<xml tag="foo">bar</xml>';
?>
 

Re: Write XML code in PHP

Posted: Tue Mar 11, 2008 8:38 am
by Largo65
anto91 wrote:Use single quotes to avoid error messaging when writing regular quotes in a varible.

Example

Code: Select all

 
<?php
echo '<xml tag="foo">bar</xml>';
?>
 
This works perfectly!:D Thanks anto91 :D

Re: Write XML code in PHP

Posted: Tue Mar 11, 2008 9:17 am
by anto91
The reason what you were trying to do was breaking the actual code

Example

Code: Select all

 
<?php
// Php thinks you ended the tag there
echo "<xml tag=" 
 
// But you continue with a value
echo "<xml tag="value"></xml>";
?>
 
Php thinks you ended the echo then comes a bunch of stuff it dose not know how to handle.

Other way of doing it

Code: Select all

 
<?php
echo "<xml tag=\"value\">sfoo</xml>";
?>
 
That is both uglier and slower and contains more characters to write.

Re: Write XML code in PHP

Posted: Tue Mar 11, 2008 9:53 am
by Sekka
anto91 wrote:Other way of doing it

Code: Select all

 
<?php
echo "<xml tag=\"value\">sfoo</xml>";
?>
 
That is both uglier and slower and contains more characters to write.
That is however the better way of doing it because,
  • You have the same problem with single quote strings. The minute the string contains a single quote, it will break. e.g. $foo = 'Welcome to Steve's party'.
  • PHP variables are not parsed in single quote strings, aka you won't be able to do $foo = "Welcome to $location.";

Re: Write XML code in PHP

Posted: Tue Mar 11, 2008 11:11 am
by anto91
Sekka wrote:
anto91 wrote:Other way of doing it

Code: Select all

 
<?php
echo "<xml tag=\"value\">sfoo</xml>";
?>
 
That is both uglier and slower and contains more characters to write.
That is however the better way of doing it because,
  • You have the same problem with single quote strings. The minute the string contains a single quote, it will break. e.g. $foo = 'Welcome to Steve's party'.
  • PHP variables are not parsed in single quote strings, aka you won't be able to do $foo = "Welcome to $location.";
For the sake of clarity i think its good that you cannot use variables in single quotes and i also think you shouldnt be able to in double its a bad habbit

Re: Write XML code in PHP

Posted: Tue Mar 11, 2008 1:24 pm
by Sekka
I'm not saying you should pass vars in that way, simply saying you can.