srider Posted February 14, 2010 Share Posted February 14, 2010 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! Link to comment https://forums.phpfreaks.com/topic/192000-sorting/ Share on other sites More sharing options...
sader Posted February 14, 2010 Share Posted February 14, 2010 sort($list); <= add this before echoing your list Link to comment https://forums.phpfreaks.com/topic/192000-sorting/#findComment-1011983 Share on other sites More sharing options...
srider Posted February 14, 2010 Author Share Posted February 14, 2010 Thank you so much for the reply. It still doesn't work. I apologize if I'm missing something easy but I really know nothing about php. I added that line: $dir = $doc_root.$web_dir; sort($list); echo utf8_encode(create_tree($dir, $web_dir)); ?> Yet it still does not work. The files are still being added into flash randomly. Any more thoughts on it? Thanks! Link to comment https://forums.phpfreaks.com/topic/192000-sorting/#findComment-1012027 Share on other sites More sharing options...
MaaSTaaR Posted February 14, 2010 Share Posted February 14, 2010 Try to put it before this line : $dir_length = count($list); Link to comment https://forums.phpfreaks.com/topic/192000-sorting/#findComment-1012031 Share on other sites More sharing options...
srider Posted February 14, 2010 Author Share Posted February 14, 2010 Success!! Thank you so much guys. I know that's a fairly simple problem but I'm sure you saved me hours of surfing!! If either of you are ever near Regina, Saskatchewan, Canada...let me know, cause I owe you a beer!! Thanks! Link to comment https://forums.phpfreaks.com/topic/192000-sorting/#findComment-1012036 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.