Jump to content

[SOLVED] save data back to xml file


rondog

Recommended Posts

I have a text area thats reading an xml file and I want to take whatever they change and save it back to the xml file. How would I got about doing that? My code looks like this so far and its displaying the xml in the text area.

<?php
$page = $_SERVER['PHP_SELF'];
$filename = "gallery.xml";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
//echo $contents;
$form = "<form action=\"$page\" method=\"get\">";
$form .= "<textarea name=\"\" cols=\"75\" rows=\"15\">$contents</textarea>";
$form .= "<input type=\"submit\" name=\"Submit\" value=\"Submit\" />";
$form .= "</form>";
echo $form;
?>

Link to comment
https://forums.phpfreaks.com/topic/83527-solved-save-data-back-to-xml-file/
Share on other sites

I dont know Im doing it wrong. Can you help me figure out what to do to write it. I have the file attributes set to 777.

 

<?php
$page = $_SERVER['PHP_SELF'];
$submit = $_POST['Submit'];
$newdata = $_POST['thexml'];
$filename = "gallery.xml";
if($submit) {
fwrite($filename,"");
fwrite($filename, $newdata);
}

$handle = fopen($filename, "w");
$content = fread($handle, filesize($filename));

$form = "<form action=\"$page\" method=\"post\">";
$form .= "<textarea name=\"thexml\" cols=\"75\" rows=\"15\">$content</textarea>";
$form .= "<input type=\"submit\" name=\"Submit\" value=\"Submit\" />";
$form .= "</form>";
echo $form;
?>

 

basically when they submit it clears whatever was in the xml file and then writes everything that was in the text area..I know its wrong but thats what im trying to achieve..

Inside the first if you need to do

$handle = fopen($filename, 'w');
fwrite($handle, $newdata);
fclose($handle);

 

Also, the fopen() you have in your code opens for writing, but you are attempting to read from it.

 

If you use PHP5 you can use file_get_contents() and file_put_contents() instead.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.