This is untested but it is the concept... I think it is was what you wanted.
Code:
<?php
function add_data($StringToAdd){
$filename = 'test.txt';
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'r+')) {
echo "Cannot open file ($filename)";
exit;
}
$contents = fread($handle, filesize($filename));
if(stripos("<item>")){
$chunk1 = substr($contents,0,stripos("<item>"));
$chunk2 = substr($contents,stripos("<item>"));
$contents = $chunk1.$StringToAdd.$chunk2;
}else{
echo "Did not find <item>";
exit;
}
if (fwrite($handle, $contents) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($contents) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>