Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.