Forum Moderators: coopster
<?php
$xml = "<?xml version=\"1.0\" encoding=\"iso-8859-2\"?>";//beginning of the xml
$xml .= "\n\r<BOOK>\n\r\t<Author>P.K.Dick</Author>\n\r\t<Title>Ubique</Title>\n\r</BOOK>";
if($fd = @fopen("books.xml", "w")) // open the desired xml file
{
fwrite($fd, $xml); //write the text to the file
fclose($fd); // you need to always close the file after writing to it.
}
?>
To do what you written you want to do is: read the xml (read as many lines as you wish, but count them), if you are on the desired line write the code, then read the rest of the file and write the variable (to which you have read the file) to the file.
Script to instert $text after certain line (not on the beginning of the file), doesn't check for the length of the file.
<?php
$f = @fopen("books.xml", "r");
$i = 0; beginning values
$line_no = 10; //after which line you want to instert text;
$text = "Text to insert";while(!feof($f))
{
$line .= trim(fgets($f));
if($++i == $line_no) $line .= $text;
}
fclose($f);
$f2 = @fopen("books.xml", "w");
fwrite($f2, $line);
fclose($f2);
$line="";//to flush the memory
?>
Best regards
Michal Cibor
[edited by: coopster at 3:14 pm (utc) on April 2, 2005]
[edit reason] joined two posts upon request [/edit]
the user is gonna write 3 fields, the first is the name of the .xml the second is a value inside the .xml(like a password) and the third is what he wants to wite inside it.