Noob Question - PHP to XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BC_PHPnewb
Forum Newbie
Posts: 4
Joined: Sun May 25, 2008 4:33 pm

Noob Question - PHP to XML

Post by BC_PHPnewb »

Hi All,
I am having trouble writing a php script to have the data be written into xml. I am trying to take the variables from flash and have it run through a php script and write into an xml file. I am trying to tweak a php script that I know works that used to send me an email but now instead of sending me an email I want it to write the data it collects into an xml file but am currently having issues. Any help at all is greatly appreciated. Thanks in advanced,
BC

Code: Select all

<?php
 
// read the variables from flash
$name = $_POST['yourname'];
$email = $_POST['youremail'];
$message = $_POST['yourmessage']; 
 
// include sender IP in the message.
$ipaddress = "Sender's IP Address: " . $_SERVER['REMOTE_ADDR'];
 
// remove the backslashes that normally appears when entering " or '
$name = stripslashes($name); 
$email = stripslashes($email); 
$message = stripslashes($message);
 
// write to xml file
if(isset($name) and isset($email) and isset($message)){
    $handle = fopen("/guestbook.xml", "w");
    fwrite($handle, $name, $email, $ipaddress, $message);
}
?>
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Noob Question - PHP to XML

Post by yacahuma »

your file write is wrong

Code: Select all

 
$xml = "<guest><name>{$name}</name>,<email>{$email}</email><ip>{$ipaddress}</ip><msg>{$message}</msg></guest>";
fwrite($handle, $xml);
 
brookisme
Forum Newbie
Posts: 3
Joined: Sun May 25, 2008 9:20 pm

Re: Noob Question - PHP to XML

Post by brookisme »

it seems there is already a simple answer but i happen to be doing the exact same thing so i thought i would go ahead and put in my solution: rather than having php insert new lines to my xml file, i load the xml file to flash, add to it/delete items/etc... in flash and then post the whole edited xml to my php script using urlvariables.

here is the php:

Code: Select all

 
<?php
$content =$_POST['newxml'];
$path = "../xml/editMe.xml";
$file = fopen($content, "w");
if(!chmod($path, 0777)) {
            echo "phpresponse=error: cannot open $path for writing";
            exit;
    } else {
            $file = fopen($path, "w+");     
}  
if(fwrite($file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>$content")) echo "phpresponse=Ok";
else echo "phpresponse=error: failed to write";
fclose($file);   
?>
 
it overwrites the whole file with the new one. in my final version i plan on using the technique to save an unedited version of the xml to a file editMe-old.xml.
cheers.brook
Post Reply