Sorting problem

Hi,

Can someone please help me out for the sorting problem for this php script? Because there is no an ability for sorting directories or files?

Any responses will be greatly appreciated it.


$path = "docs/";

	function createDir($path = '.')
	{	
		if ($handle = opendir($path)) 
		{
			echo "<ul>";
		
			while (false !== ($file = readdir($handle))) 
			{
				if (is_dir($path.$file) && $file != '.' && $file !='..')
					printSubDir($file, $path, $queue);
				else if ($file != '.' && $file !='..')
					$queue[] = $file;
			}
			
			printQueue($queue, $path);
			echo "</ul>";
		}
	}
	
	function printQueue($queue, $path)
	{
		foreach ($queue as $file) 
		{
			printFile($file, $path);
		} 
	}
	
	function printFile($file, $path)
	{
		echo "<li><a href=\\"".$path.$file."\\" target=\\"_blank \\">$file</a></li><br><br>";
	}
	
	function printSubDir($dir, $path)
	{
		echo "<li><span class=\\"toggle\\">$dir</span>";
		createDir($path.$dir."/");
		echo "</li>";
	}
	
	createDir($path);


[FPHP]scandir[/FPHP] sorts.

The main problem is that you didn’t tell us in what order you want the files/folders sorted.

Both glob() and [url=http://php.net/scandir]scandir() can return sorted arrays, if “alphabetical” order is fine. For any other sorting, pile up the files/folders into an array and sort the array using one of the many [url=http://php.net/array.sorting]array sorting functions available.

I am sorry didn’t tell you guys what kind of sorting I was looking for.

If there is not too much to ask I’d like the alphebetical by ascendant or descendant.

Thanks so much for your help.

The links that we gave should push you in the right direction. Try those and let us know how you get on.

Sorry. I have tried, it didn’t work.


$path = "docs/"; 

    function createDir($path = '.') 
    {     
        if ($handle = opendir($path))  
        { 
            echo "<ul>"; 
         
            while (false !== ($file = readdir($handle)))  
            { 
                if (is_dir($path.$file) && $file != '.' && $file !='..') 
                    printSubDir($file, $path, $queue); 
                else if ($file != '.' && $file !='..') 
                    $queue[] = $file; 
            } 
             
            printQueue($queue, $path); 
            echo "</ul>"; 
        } 
    } 
     
    function printQueue($queue, $path) 
    { 
        foreach ($queue as $file)  
        { 
            printFile($file, $path); 
        }  
    } 
     
    function printFile($file, $path) 
    { 
        echo "<li><a href=\\"".$path.$file."\\" target=\\"_blank \\">$file</a></li><br><br>"; 
    } 
     
    function printSubDir($dir, $path) 
    { 
        echo "<li><span class=\\"toggle\\">$dir</span>"; 
        createDir($path.$dir."/"); 
        echo "</li>"; 
    } 
     uksort($array, 'strcasecmp');
    createDir($path);