Jump to content

file uploader that writes to a xml


pagalo

Recommended Posts

Hi,

I am making a dynamic xml gallery. I am using flash to read an xml and then display the images and descriptions.

The problem is with my php code that writes to the xml, the 'location' node is only stored for the last node while all of the previous nodes are emptied.

[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Adding/Deleting Images...</title>
<style type="text/css">
em {
text-align:center;
}
</style>
</head>
<body>
<?

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['path']['name']);

if(move_uploaded_file($_FILES['path']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['path']['name']).
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

$images = Array();
function start_element($parser, $name, $attrs){
global $images;
if($name == "image"){
array_push($images, $attrs);
}
}
function end_element ($parser, $name){}
$images_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, $images_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "ins"){
array_push($images, Array(
"description" => $_POST['description'],
"location" => "$target_path"));
$images_final = $images;
}else if($_POST['action'] == "del"){
$images_final = Array();
foreach($images as $image){
if($image['path'] != $_POST['path']){
array_push($images_final, $image);
}
}
}
$write_string = "<gallery>";
foreach($images_final as $image){
$write_string .= "<image path=\"$image[location]\" description=\"$image[description]\" />";
}
$write_string .= "</gallery>";
$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=\"index.php\" title=\"return\">Return</a>";
?>
</body>
</html>
[/code]


here is the page that lets the user upload a file and a descritpion.

[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Add Images</title>
<style type="text/css">
table {
border:1px dotted #ccc;
width:100%;
}
td {
background-color:#aaa;
color:#fff;
}
fieldset { border:0; }
label { margin-right:5px; }

</style>
</head>
<body>
<form enctype="multipart/form-data" action="processForm.php" method="post">
<fieldset>

<input type="hidden" name="MAX_FILE_SIZE" value="200000" />
<label for="path">Upload a file:</label> <input type="file" id="path" name="path" /> <br />
<label for="description">description:</label><input type="text" id="description" name="description" /><br />

<select name="action">
<option value="ins">Insert</option>
<option value="del">Delete</option>
</select>
<input type="submit" value="Upload File" />
</fieldset>
</form>
<p>Current entries:</p>
<?php
function start_tag($parser, $name, $attrs){
global $table_string;
$table_string .= "<tr><td>$attrs[path]</td><td>$attrs[description]</td></tr>";
}
function end_tag($parser, $name){}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($parser, "start_tag", "end_tag");
$table_string = "<table>
<tr><th>Image</th><th>Description</th></tr>";
xml_parse($parser, file_get_contents("images.xml")) or die("Error parsing XML file");
$table_string .= "</table>";
echo $table_string;

?>
</body>
</html>[/code]

cheers i hope someone can help!
Link to comment
Share on other sites

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.