(This assumes that the images directory contains file type icons files such as "directory.png", "file.png", "php.png", "html.png", etc.)
DirList.php:
<?php
class DirList
{
private $dir;
private $listing;
public function DirList($dir)
{
if(!is_dir($dir))
{
user_error("DirList(): '$dir' is not a valid directory");
return(FALSE);
}
$this->dir = $dir;
$this->buildList();
uasort($this->listing, array($this, 'SortList'));
}
public function getDirName()
{
return($this->dir);
}
public function getDirList()
{
return($this->listing);
}
private function buildList()
{
$files = glob($this->dir . '/*');
foreach($files as $file)
{
$name = basename($file);
if(is_dir($file))
{
$this->listing[$name] = array(
'type' => 'directory',
'size' => ''
);
}
else
{
$this->listing[$name]['type'] = $this->getType($file);
$this->listing[$name]['size'] = filesize($file);
}
}
}
public function getType($file)
{
if(is_dir($file))
{
return('directory');
}
$parts = explode('.', basename($file));
return((count($parts) > 1) ? strtolower(array_pop($parts)) : 'file');
}
private function sortList($a, $b)
{
if($a['type'] == 'directory' and $b['type'] != 'directory')
{
return -1;
}
elseif($b['type'] == 'directory' and $a['type'] != 'directory')
{
return 1;
}
else
{
return(strcmp($a, $b));
}
}
}
Sample usage:
require_once "DirList.php";
$test = new DirList($_SERVER['DOCUMENT_ROOT'].'/test');
$imgDir = $_SERVER['DOCUMENT_ROOT'].'/images/';
echo "<h2>Listing for /test:</h2>\n";
echo "<ul>\n";
foreach($test->getDirList() as $name => $info)
{
printf(
"<li><img src='/images/%s' alt='%s'> %s %s</li>\n",
(file_exists($imgDir.$info['type'].'.png')) ?
$info['type'].'.png' :
'file.png', // default image
$info['type'],
$name,
($info['type'] != 'directory') ? "({$info['size']} bytes)" : ''
);
}
echo "</ul>\n";