Jump to content


Photo

file uploader that writes to a xml


  • Please log in to reply
No replies to this topic

#1 pagalo

pagalo
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 21 October 2006 - 09:39 AM

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.

<!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>


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

<!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>

cheers i hope someone can help!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users