XML Directory Lists (With Php)
October 15th, 2009
Here is a snippet of php code that when run, will produce an XML markup of the images contained within a directory.
This is great is you are building a flash sideshow or similar and want to remove the dependency that comes with hand coding XML files.
I added a bunch of comments, feel free to let me know if anything needs explaining in greater detail.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?php function DirDisplay($LocDir) { // list of valid image extensions to look for $valid_ext[0] = "jpg"; $valid_ext[1] = "png"; $valid_ext[2] = "gif"; $valid_ext[3] = "bmp"; // init XML output print '<?xml version="1.0" encoding="UTF-8"?>'."\n"; print "<Images>"."\n"; // open directory $TrackDir=opendir($LocDir); // loop through files while ($file = readdir($TrackDir)) { // check for non-parent file names if (!($file == "." || $file == "..")) { // reads file extension $ext = substr(strrchr($file, '.'), 1); // makes sure file has allowed extension if (in_array($ext,$valid_ext)) { // adds file to XML output print "\t".'<Image>'; print '<![CDATA['.$file.']]>'; print '</Image>'."\n"; } } } // end XML output print "</Images>"."\n"; // close open directory closedir($TrackDir); return; } @ DirDisplay("."); ?> |
And here is an example output. I’ll post up a demo using this xml markup in a future post.
1 2 3 4 5 6 7 | <?xml version="1.0" encoding="UTF-8"?> <Images> <Image><![CDATA[Image_01.jpg]]></Image> <Image><![CDATA[Image_02.jpg]]></Image> <Image><![CDATA[Image_03.jpg]]></Image> <Image><![CDATA[Image_04.jpg]]></Image> </Images> |
Here’s a link to download the file, hope it helps!
Comment (0)
