1

I am currently using SimpleXML to reading an xml file to generate my first array. This array displays a list of images that are found in a directory and written to xml file.

This has worked for me in the past until I upload new photos to the same album. I like to know what one does to get a complete list of images that got uploaded to the end of that first array. Any suggestions?

XML File:

<album>
<image path="/albums/1/images/100_4124.jpg" />
<image path="/albums/1/images/100_4307.jpg" />
<image path="/albums/1/images/100_4335.jpg" />
</album>

SimpleXML to get array:

$xml = simplexml_load_file('/albums/1/photos.xml');
foreach ($xml->image as $image)  {
    echo '<li><img src="'.$image.'"></li>';
}

What I am getting for my results:

<li><img src="/albums/1/images/100_4124.jpg"></li>
<li><img src="/albums/1/images/100_4307.jpg"></li>
<li><img src="/albums/1/images/100_4335.jpg"></li>

Scanning the directory for all images:

if ($handle = @opendir('/albums/1/')) {
    $filenames = array();
    while (false !== ($file = readdir($handle))) {
        $ext = substr($file, strrpos($file, '.') + 1);
        if ($file != '.' && $file != '..') {
            $filenames[] = $file;
            $total++;
        }
    }
}
closedir($handle);

foreach ($filenames as $filename) {
    echo '<li><img src="'.$filename.'"></li>';
}

What I would like to get for results using both arrays:

<li><img src="/albums/1/images/100_4124.jpg"></li>
<li><img src="/albums/1/images/100_4307.jpg"></li>
<li><img src="/albums/1/images/100_4335.jpg"></li>    

<li><img src="/albums/1/images/100_9000.jpg"></li>
<li><img src="/albums/1/images/100_9001.jpg"></li>
<li><img src="/albums/1/images/100_9002.jpg"></li>

The last 3 images that are missing from xml file would be added it to the end of the list.

2
  • Why not just adjust the XML file? Commented Jan 17, 2010 at 21:57
  • You know what Anon, that is the solution to the true problem. Commented Jan 17, 2010 at 21:59

2 Answers 2

1

Perhaps this would work for you?

<?php

$images = array();

foreach ($xml->image as $image) {
    $images[] = $image;
}

foreach (array_diff($filenames, $images) as $image) {
    $images[] = $image;
}

foreach ($images as $image) {
    echo '<li><img src="' . $image . '" /></li>';
}

?>

This is assuming that $image from the $xml->image and $filename from your dir search are formatted the same. From what you've shown they are. If they aren't should be easy enough to run them through a quick regex to get them in a state where they are comparable.

Any who it looks like array_diff() is what you're looking for.

Sign up to request clarification or add additional context in comments.

1 Comment

Beautiful, that worked like a charm and it was easy to implement with what I already had.. Thanks
1

If you get a full list of the file names in the directory you could use PHP in_array and search for it in an array of your paths

$paths = array();
foreach ($xml->image as $image) {
    $paths[] = $image['path'];
}
foreach ($filenames as $filename) {
    if (!in_array($filename, $paths) {
        $newImage = $xml->addChild('image');
        $newImage->addAttribute('path', $filename);
    }
}

//  save adjusted xml file, assuming you have permission
$xml->asXML('/albums/1/photos.xml');
foreach ($xml->image as $image)  {
    echo '<li><img src="'.$image.'"></li>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.