Auto Generate XML from image directory

Hello,

I’m a complete PHP newb. I’m curious whether PHP is even the best solution for my problem.
I’m trying to automatically generate an xml file with the following structure:

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

<categories>
	<category id="film">Films</category>
	<category id="photo">Photography</category>
	<category id="graphic">Graphic Design</category>
	<category id="web">Web Design</category>
	
</categories>

<items>
	<item>
		<thumbnail>img/photo/003_small.jpg</thumbnail>
		<preview>img/photo/003.jpg</preview>
		<category>photo,film</category>
		<description>Description goes here.</description>
	</item>
	<item>
		<thumbnail>img/photo/003_small.jpg</thumbnail>
		<preview>img/photo/003.jpg</preview>
		<category>film</category>
		<description>Description goes here.</description>
	</item>
	<item>
		<thumbnail>img/photo/003_small.jpg</thumbnail>
		<preview>img/photo/003.jpg</preview>
		<category>graphic,photo</category>
		<description>Description goes here.</description>
	</item>
</items>

[/code]

(You can ignore the description as I can put that in manually after the fact)
There are lots of images and I just know that theres a way to fascilitate a script for this repetitive task to read a directory of a folder named “img” - see that there are a few folders named “photo” “film” and “graphic” etc, pull the images out of them in their corresponding categories etc.
I just need the initial generated script with thumbnails and all the attributes, I can do the final tweeks afterward.

This is for http://www.zoomrix.com/#portfolio

All the help would be greatly appreciated, even if you point me to the right tutorial.

Thanks,

GeorgeKotelnikov
www.zoomrix.com

Hi George ;D

Here is a little script i wrote to demonstrate the use of PHP and repetitive tasks:
[php]<?php
// for this example, lets set some arrays.
// (on a live site, this info would come from another source such as a database etc…)
$categories = array(‘film’, ‘photo’, ‘graphic’, ‘web’); // categories array
$items = array(‘001.jpg’, ‘002.jpg’, ‘003.jpg’, ‘004.jpg’); // items array
$images = array(‘001_small.jpg’, ‘002_small.jpg’, ‘003_small.jpg’, ‘004_small.jpg’); // images array

// begin the categories section.
echo “\r\n”;
// loop through the array…
foreach($categories as $category)
{
// … and display each category.
echo “\t<category id=”$category">" . ucwords($category) . “\r\n”;
}
// end the categories section.
echo “\r\n”;

// begin the items section.
echo “\r\n”;
$i=0; // set a counter at zero. (i’m using this for the image thumbnails)
// loop through the array…
foreach($items as $item)
{
// … and display each item.
echo “\t\r\n”;
echo “\timg/photo/$images[$i]\r\n”;
echo “\timg/photo/$item\r\n”;
echo “\t$categories[$i]\r\n”;
echo “\tDescription goes here.\r\n”;
echo “\t\r\n”;
$i++; // increment the counter.
}
// end the items section.
echo “\r\n”;

?>
[/php]
notice in the script i use \t and \r\n.
these set the text one tab space in, and return the carriage to a new line. (i used these for display purposes)

[tt]// outputs:

Film
Photo
Graphic
Web

img/photo/001_small.jpg img/photo/001.jpg film Description goes here. img/photo/002_small.jpg img/photo/002.jpg photo Description goes here. img/photo/003_small.jpg img/photo/003.jpg graphic Description goes here. img/photo/004_small.jpg img/photo/004.jpg web Description goes here. [/tt]

I hope that gives a little insight into how php can help.

Note: this script is only an example and by no means should be used on a live site without a little modification to make it more secure.

Red. :wink:

Thanks for your reply. Well this is the PHP code I’m working from right now.

[php]<?php
$path_to_image_dir = ‘images2’; // relative path to your image directory

$xml_string = <<<XML

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

XML;

$xml_generator = new SimpleXMLElement($xml_string);

if ( $handle = opendir( $path_to_image_dir ) )
{
while (false !== ($file = readdir($handle)))
{
if ( is_file($path_to_image_dir.’/’.$file) )
{

       $item = $xml_generator->addChild('item');  
       $item->addChild('thumbnail', $path_to_image_dir.'/thumbs/'.$file);    
       $item->addChild('preview', $path_to_image_dir.'/'.$file);   
       $item->addChild('category', 'photo'); 
       $item->addChild('description', ' ');     
    }
}
closedir($handle);

}

header(“Content-Type: text/xml”);

echo $xml_generator->asXML();
?>[/php]

I just can’t figure out how to include the and tags.

Any help appreciated.

Sponsor our Newsletter | Privacy Policy | Terms of Service