sorting problem in PHP script

This is where you share YOUR scripts with others

Moderators: egami, macek, gesf

Post Reply
begeiste
New php-forum User
New php-forum User
Posts: 83
Joined: Thu Jun 03, 2004 11:27 am

Tue Aug 16, 2011 10:51 am

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.

Code: Select all

$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);

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2192
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Wed Aug 17, 2011 4:15 am

There is, you just create an array from the file listing, then sort the array.

Post Reply