Hi,

I have some code which builds an xml scheme from the file system. Everything works great except i need the folders to be sorted alphanumerically - anyone know how to do this?

<?php

$indir = "../galleries";
$files = opendir($indir);
$outxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n";
$outxml .= "<galleries>\n\n";	
while ($file = readdir($files)) {
  if(is_dir($indir.'/'.$file))
  if ($file != '.' && $file != '..') {
    {
    $outxml .= get_folder_xml($indir.'/'.$file,$file);
    }
	}
  } 
$outxml .= "</galleries>";

closedir($files);  
$fp = fopen("../xml/gallery/gallery.xml", "w"); fwrite($fp, $outxml); fclose($fp); function get_folder_xml($imageDir,$title) { $extensions = array(".jpg", ".jpeg", ".JPG",".JPEG",".gif",".GIF",".swf",".SWF"); $output = ""; if($folder = opendir($imageDir)) { $filenames=array(); while (false !== ($file = readdir($folder))) { $dot = strrchr($file, '.'); if(in_array($dot, $extensions)) { array_push($filenames, $file); } } $spaceTitle = str_replace("_"," ",$title); $newPath = str_replace("../","",$imageDir); $output .= "<gallery>\n"; $output .= "<title>$spaceTitle</title>\n"; $output .= " <images>\n"; foreach ($filenames as $source) { $imageName = str_replace("mainThumb_","",$source); $finalName = str_replace("_"," ",$imageName); $imageName = str_replace($extensions,"",$finalName); $output .= "\t <image src=\"$newPath/$source\" thumb=\"$newPath/thumbs/$source\">$imageName</image>\n"; } $output .= " </images>\n"; $output .= "</gallery>\n\n"; return $output; closedir($folder); } } ?>

thanks,
g

    The way you currently have your code, no, there is no way to sort like that. What should happen is the way that the filesystem sorts the folder entries is how it should be returned.

    If you wanted to sort things, then I would say you need to add in a step between the reading of an entry and the creation of the XML. That step would involve all entry names being saved to an array and then using [man]natsort/man on that array to sort the entries "naturally". Now if you have sub-folders, then you'd need to take it a step further and actually move the folders around as you needed so they're either listed first, last, or intertwined with the entries.

      doh! thought this would be easy :-(

      thanks for your help

        Write a Reply...