0

I'm trying to modify a drag and drop uploader. Once the file has been uploaded it shows up on right-hand side. I want to add a description field for each file on the right-hand side. My XML is being written by checking the directory. I am able to save each file's descriptions to the XML. But it overwrites all of my descriptions with "-" if I were to remove or upload a file to the directory. What I'm try to do is upload or remove new item without overwriting my older items descriptions.

PHP- for remove and upload. this function is checking the directory and adding nodes to XML for each file that been uploaded.

if ( $handle = opendir( $path_to_image_dir ) )
{
    while (false !== ($file = readdir($handle)))
    {
        if ( is_file($path_to_image_dir.'/'.$file) && $file != "." && $file != ".." && $file != "Thumb.db" && $file != "Thumbs.db" && $file != ".DS_Store" )
        {
            list( $width, $height ) = getimagesize($path_to_image_dir.'/'.$file);
            $image = $xml_generator->addChild('image');
            $image->addChild('id', 'file'.$i++);
            $image->addChild('name', $file);
            $image->addChild('width', $width);
            $image->addChild('height', $height);
            $image->addChild('description', '-');
        }
    }
    closedir($handle);
}

// Write the XML File to a file.
$xml_generator->asXML('../file.xml');

Description.Php adding the descriptions to each item.

$filename = "../file.xml";

//value form the pop up description form.
$description = $_POST['description'];

// id of input from the write rightside list. 
$hid = $_POST['fileID'];

$xml->image ->description = $description;

$xml = simplexml_load_file($filename);

foreach($xml->image as $image)
{
    // matching the xml id to input id, and then updating the description. 
    if ($image->id == $fileID)
    {
        $image->description = $description;
    }
}

$xml->asXML($filename);

XML

<images>
  <image>
    <id>file0</id>
    <name>image1.jpg</name>
    <height>1435</height>
    <width>2551</width>
    <description>-</description>
  </image>
  <image>
    <id>file1</id>
    <name>Image2.jpg</name>
    <height>1435</height>
    <width>2551</width>
    <description>-</description>
  </image>
</images>
Prix
  • 19,417
  • 15
  • 73
  • 132
Tierendu
  • 29
  • 4
  • 1
    what does `echo $image->id;` and `echo $description;` give you in **Description.Php**? Do they display? --- I'm just asking because the code in your question does not look that wrong. If you can compact the example so it cab be simply executed without file-interaction that would be good to improve the question. In the end it's about XML and not a file upload, so the title might draw some users away, too. – hakre Jul 13 '13 at 07:49
  • echo $image->id; and echo $description; is for me grab the value on ajax request to console log. You can ignore those. It doesn't really do anything. I'm sorry,it must of confused you.You might be right about the title and question. Thanks! – Tierendu Jul 15 '13 at 16:54
  • @Prix file0 is the id of my html input. then I'm matching that to the xml id->file0. So it updates the only file0 section in xml. This part already works for me. – Tierendu Jul 15 '13 at 16:59

1 Answers1

0

I can see 3 issues with your Description.Php, first you have the variable $hid that should have been called $fileID, and you also call $xml->image ->description = $description; before the simplexml_load_file and last you do not verify if fileID and description are set or not.

So your file should be like this:

$filename = "../file.xml";

//value form the pop up description form.
$description = $_POST['description'];
if (!isset($description))
    die("The field description is not set.");

// id of input from the write rightside list. 
$fileID = $_POST['fileID'];
if (!isset($fileID))
    die("The field fileID is not set.");

$xml = simplexml_load_file($filename);

foreach($xml->image as $image)
{
    // matching the xml id to input id, and then updating the description. 
    if ($image->id == $fileID)
    {
        $image->description = $description;
    }
}

$xml->asXML($filename);

You should also use isset or similar to assure that the POST exist.

In regards your first code, what happens is that without reading the XML you already have you create new nodes for each files over and over and save as a new XML overwriting the old one in order to keep the old descriptions you should do something like this:

if ( $handle = opendir( $path_to_image_dir ) )
{
    while (false !== ($file = readdir($handle)))
    {
        if ( is_file($path_to_image_dir.'/'.$file) && $file != "." && $file != ".." && $file != "Thumb.db" && $file != "Thumbs.db" && $file != ".DS_Store" )
        {
            $fileID = 'file'.$i++;
            list( $width, $height ) = getimagesize($path_to_image_dir.'/'.$file);
            $oldImage = $xml_generator->xpath('//images/image[id="'.$fileID.'"]');
            if (count($oldImage) == 0)
            {
                $image = $xml_generator->addChild('image');
                $image->addChild('id', $fileID);
                $image->addChild('name', $file);
                $image->addChild('width', $width);
                $image->addChild('height', $height);
                $image->addChild('description', '-');
            }
            else
            {
                $oldImage = $oldImage[0];
                $oldImage->name = $file;
                $oldImage->width = $width;
                $oldImage->height = $height;
            }
        }
    }
    closedir($handle);
}

// Write the XML File to a file.
//$xml_generator->asXML('../file.xml');
//In case it saves all in one line and u want 
//to properly format the XML use:
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml_generator->asXML());
echo $dom->save('../file.xml');
Prix
  • 19,417
  • 15
  • 73
  • 132
  • Oh it was a syntax issue on my part. I changed some of the variable when posting the example I was suppose to change to `$fileID`, but it has the same variable on both place in the real code. I tried out your code. It works just as my code, but I see your way is much more cleaner. It work fine changing just descriptions. However It doesn't fix my initial issue. It is still getting overwritten when I upload or modify(remove) a file.Then all the descriptions turn to `-`. I really appreciate your help – Tierendu Jul 15 '13 at 19:01
  • @Tierendu see update, given that $xml_generator is reading the `../file.xml` then we can use that to match existent images and simple update it instead of recreating the entire file with empty descriptions. – Prix Jul 15 '13 at 19:25
  • I had a feeling that was the issue, couldn't really come up with a game plan to fix the issue. Thanks! – Tierendu Jul 15 '13 at 19:30
  • No worries, I won't leave you hanging. If it is correct I will mark it, else I will let you know. I'm testing the code now. Thanks a lot. – Tierendu Jul 15 '13 at 20:02
  • Thank you so much. It works great now. I was trying to figure this part for days. – Tierendu Jul 15 '13 at 20:41
  • I have a problem with deleting the last item on list. For some reason it doesn't delete from XML. Can you help me? Thanks! – Tierendu Jul 17 '13 at 23:40
  • @Tierendu post a new question with the issue and drop me a comment with the link and I will check out. – Prix Jul 18 '13 at 01:36
  • hi @Prix I posted another questions. XML has been changed little, I no longer have "file" on XML ID node and Value from HTML. Everything works the way it suppose, but last item. Thanks, Here is the link. http://stackoverflow.com/questions/17731198/last-xml-items-isnt-removable-from-the-xml – Tierendu Jul 18 '13 at 18:35