Hi,
I really don't know much about php but need a quick fix. The code below creates a list of images in a directory and outputs an XML file. The only issue I have is that they are not sorted. Can anyone tell me how to change this file so that it outputs the image names to the XML file alphabetically?
<?php
require_once('JPEG-Meta.php');
header('Content-type: text/xml');
function create_tree($file_dir, $web_dir){
$masterXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$fileXML = "";
if ($handle = @opendir($file_dir)){
$i=0;
while (false !== ($file = @readdir($handle))){
if ($file != "." && $file != ".."){
$list[$i]=$file;
$i++;
}
}
$dir_length = count($list);
$dirXML = "<gallery>\n";
for($i=0; $i<$dir_length; $i++){
if(strrpos($list[$i], ".") != FALSE && strrpos($list[$i], ".jpg") != FALSE){
$jpegMeta = new JPEG($file_dir.$list[$i]);
$caption = $jpegMeta->getIPTCField("Caption");
$metaWidth = $jpegMeta->getWidth();
$metaHeight = $jpegMeta->getHeight();
$info = getimagesize($file_dir.$list[$i]);
$imgW = $info[0] > 0 ? $info[0] : $metaWidth;
$imgH = $info[1]> 0 ? $info[1] : $metaHeight;
$fileXML .= "\t<img width=\"".$imgW."\" height=\"".$imgH."\"
src=\"".$web_dir.$list[$i]."\" caption=\"".$caption."\" filename=\"".$list[$i]."\" />\n";
}
}
$masterXML .= $dirXML.$fileXML."</gallery>";
closedir($handle);
}
return $masterXML;
}
$web_dir = $_GET['dir'];
if(substr($web_dir, 0, 1) == "/"){
$doc_root = str_replace($_SERVER['PHP_SELF'], '', str_replace('\\', '/',$_SERVER["SCRIPT_FILENAME"]));
} else {
$doc_root = str_replace(basename($_SERVER['PHP_SELF']), '', str_replace('\\', '/',$_SERVER["SCRIPT_FILENAME"]));
}
if(substr($web_dir, -1) != "/"){
$web_dir .= "/";
}
$dir = $doc_root.$web_dir;
echo utf8_encode(create_tree($dir, $web_dir));
?>
Thanks a lot!