this is my xml file its using in flash image viewer

<?xml version="1.0" encoding="UTF-8"?>

<gallery>
<image url="Flower Photo 1.jpg" />
<image url="Flower Photo 2.jpg" />
<image url="Flower Photo 3.jpg" />
<image url="Flower Photo 4.jpg" />
<image url="Flower Photo 5.jpg" />
<image url="Flower Photo 6.jpg" />
<image url="Flower Photo 7.jpg" />
<image url="Flower Photo 8.jpg" />
<image url="Flower Photo 9.jpg" />
<image url="Flower Photo 10.jpg" />
</gallery>

what i want is add new image tag using php form

Dani AI

Generated

A simple, robust way to append an image element is to work with the XML directly instead of converting to arrays. 's conversion idea is valid when you need heavy restructuring, but for a small gallery append SimpleXML (or DOMDocument if you must preserve a specific XML prolog/formatting) is much simpler and less error-prone.

A minimal, safe workflow (load, validate input, add element, write atomically):

<?php
$xml = simplexml_load_file('gallery.xml');
if ($xml === false) { /* handle load error */ exit; }

$filename = basename($_POST['filename']);                       // strip paths
if (!preg_match('/^[A-Za-z0-9 _.-]+\.(jpg|jpeg|png|gif)$/i', $filename)) {
    /* invalid filename */ exit;
}

$new = $xml->addChild('image');
$new->addAttribute('url', $filename);

// atomic write: save to temp file with LOCK_EX then rename
$tmp = tempnam(sys_get_temp_dir(), 'gxml');
file_put_contents($tmp, $xml->asXML(), LOCK_EX);
rename($tmp, 'gallery.xml');
?>

Key points and common pitfalls not mentioned in the thread:

  • Always sanitize and validate the incoming filename. Prefer storing filenames from server-side uploads rather than arbitrary user-supplied URLs.
  • Use atomic writes (temp + rename or file_put_contents with LOCK_EX) to avoid race conditions when multiple requests update the file.
  • Ensure gallery.xml is readable and writable by the web process and keep a backup before batch edits.
  • Watch encoding/BOM issues: Flash and other clients often expect UTF-8 without a BOM. Use DOMDocument if you need explicit control over the XML declaration/encoding.
  • If updates must be concurrent-safe at higher scale, use a small database or a lock file (flock) rather than a single XML file.

This keeps the code short, avoids unnecessary conversions, and addresses file-safety and security concerns that are easy to miss. It expands on 's high-level idea with concrete, practical safeguards.

Recommended Answers

All 2 Replies

Try following these steps:

1. Read the XML file content into a PHP array.
There are many ways of doing this. I'd recommend the use of a couple of simple functions for converting between XML data and PHP arrays. (Google "php array to xml")

2. Add the new image to the array.
Then sort the array (if order is of any concern).

3. Convert the array back into XML and write that out to the XML file.
(Use the function/s from #1)

Hope that helps.

-Dave

ok thanx ill try

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.