[php xml] trouble formatting xml from php

I’m quite new to php and xml and am trying to use some scripts to write to an xml form. The trouble is I there are 2 things I can’t get my head around. The first is I cant format the xml theway I want, I’ve seen others do it but when i try it just doesn’t write to the xml file.

<?
$uploaddir = "photo_gallery"; 

if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
$images = Array();
function start_element($parser, $file, $attrs){
    global $images;
    if($file == "image"){
        array_push($images, $attrs);
    }
}
function end_element ($parser, $file){}
$playlist_string = file_get_contents("images.xml");
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse($parser, $playlist_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "ins"){
    array_push($images, Array(
                "gallery" => $_POST['gallery'],
                "title" => $_POST['title'],
                "date" => $_POST['date'],
                "file" => $_POST['name'])
            );
    $images_final = $images;
}else if($_POST['action'] == "del"){
    $images_final = Array();
    foreach($images as $image){
        if($image['gallery'] != $_POST['gallery']){
            array_push($images_final, $image);
        }
    }
}
$write_string = "<images>
";
foreach($images_final as $image){

$write_string .= "	<image gallery=\"$image[gallery]\" title=\"$image[title]\" date=\"$image[date]\" file=\"$image[name]\" />
";
}
$write_string .= "</images>
";
$fp = fopen("images.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);


print "<em>image inserted or deleted successfully :)</em><br />";
print "<a href=\"admin.php\" title=\"return\">Return</a>";
?>

The second as you may see from here is that I want to take the name of the image i’m uploading to the server and place it in the xml too.

The way I want to format the xml is… Exactly as from the Kirupa tutorial on an xml and flash photo gallery.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
    <pic>
        <image>http://www.kirupa.com/developer/mx2004/pg/kresge.jpg</image>
        <caption>Kresge</caption>
    </pic>
    <pic>
        <image>http://www.kirupa.com/developer/mx2004/pg/medialab.jpg</image>
        <caption>Media Lab</caption>
    </pic>
</images>

Any help would be greatly appretiated, been trying on and off on this for over a week now.

Dave