Hi All,
I found this bit of code on the net and it does exactly what I want except I can't figure out how to sort the output in sequence. This code simply displays the folders in a specified folder. I'd like to be able to have the output display in folder name alph order. Could someone please lend a hand?
Cheers
Steph.
<?php
//require_once("../stat.php");
define( SELFNAME, basename($_SERVER['SCRIPT_NAME']));
define( SELFPATH, dirname($_SERVER['SCRIPT_NAME']));
define( DATE_FORMAT, "d-M-Y H:i");
define( DIR_SIZE, "<DIR>");
define( CM_ITEM_NAME_MAX, 30);
define( VM_ITEM_NAME_MAX, 25);
define( VM_TABLE_COLS, 4);
define( PREVIEWS_DIR, "!icons/");
define( DEFAULTS_DIR, "defaults/");
define( PREVIEWS_EXT, "gif,jpg,png");
define( DEFAULTS_EXT_PREFIX, '$');
define( DIRLIST_ICON, "!dirlist.gif");
define( PREVIEW_ICON, "!preview.gif");
define( DEFAULT_DIR_ICON, "!dir");
define( DEFAULT_FILE_ICON, "!file");
define( DEFAULT_MODE, 0);
define( DEFAULT_PREVIEWS_DIR, PREVIEWS_DIR);
define( DEFAULT_DEFAULTS_DIR, DEFAULT_PREVIEWS_DIR.DEFAULTS_DIR);
// You can disable any of the following definitions by changing it into comment line.
// Note: There is no FALSE option for boolean type constant definition!
//define( ALLOWED_EXT, ".bmp.gif.jfif.jif.jpe.jpeg.jpg.pcx.png.psd.tga.tif.tiff.wbmp."); // list filenames with these extensions only
define( DENIED_EXT, ".htm.html.php.php3.phtml.shtml.xhtml.log.ini."); // do not list filenames with these extensions
define( SHOW_DIR, TRUE); // enable listing of subdirectories
//define( SHOW_PREVIEWS_DIR, TRUE); // enable listing of thumbnails directory
//define( SHOW_THE_SCRIPT, TRUE); // include this script in the listing
define( ENABLE_VISUAL_MODE, TRUE); // enable preview/thumbnail/visual mode
$D = $_GET['D'];
$G = $_GET['G'];
$D = substr_replace( $D, '', 0, strspn( $D, './\\')); // ocisti zaciatok
$D = strrev($D);
$D = substr_replace( $D, '', 0, strspn( $D, './\\')); // ocisti koniec
$D = strrev($D);
$D = strtr( str_replace( '..', '', $D), '\\', '/'); // este vo vnutri
if ($D != '')
{
if (!defined('SHOW_DIR') || !is_dir($D) || (!defined('SHOW_PREVIEWS_DIR') && stristr( '/'.$D.'/', '/'.PREVIEWS_DIR)))
{
header( "HTTP/1.0 404 Not Found");
die( "<html><body><b>\nOoops!<br>\nNo such directory in current scope.<br>\n<br><a href=\"".SELFNAME."\"><index></a></b>\n</body></html>"); // niekto nas skusa ;-)
}
$PARENT = substr( $D, 0, strrpos( $D, '/'));
if ($PARENT != '')
$PARENT .= '/';
else
unset($PARENT);
$D .= '/';
}
else
{
unset($D);
}
if (defined('ENABLE_VISUAL_MODE'))
{
if (!isset($G))
$G = DEFAULT_MODE;
if ((substr( PREVIEWS_DIR, 0, 1) == '/') || (substr( PREVIEWS_DIR, 0, 2) == '..'))
define( PREVIEWS_PATH, PREVIEWS_DIR.$D);
else
define( PREVIEWS_PATH, $D.PREVIEWS_DIR);
define( DEFAULTS_PATH, PREVIEWS_PATH.DEFAULTS_DIR);
}
else
{
unset($G);
}
//header( "Expires: " . gmdate("D, d M Y H:i:s") . " GMT");
//header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta name="Generator" content="DirList 2004.02.19">
<meta name="Description" content="generated directory listing">
<meta name="author" content="(c) martinko 2001,2003,2004 / ga-mato.szm.sk">
<title>index of <?php if (strlen(SELFPATH) > 1) echo SELFPATH; echo '/'.$D.' at '.$_SERVER['SERVER_NAME'] ?></title>
</head>
<body>
<h2><?php
if (defined('ENABLE_VISUAL_MODE'))
{
echo '<a href="'.SELFNAME.'?G=' . (empty($G) ? '2' : '0');
if ($D != '')
echo '&D=' . rawurlencode( substr( $D, 0, -1));
echo '"><img src="'.DEFAULT_DEFAULTS_DIR;
if (empty($G))
echo PREVIEW_ICON.'" alt="switch to visual mode" title="switch to visual mode"';
else
echo DIRLIST_ICON.'" alt="switch to classic mode" title="switch to classic mode"';
echo ' border="0" align="right"></a>';
}
?>Index of /<?php
if (strlen(SELFPATH) > 1)
echo substr( SELFPATH, 1, strrpos( SELFPATH, '/')) . '<a href="'.SELFNAME.'?G=' . (empty($G) ? '0' : '2') . '">' . substr( SELFPATH, strrpos( SELFPATH, '/') + 1) . '</a>/';
echo dir_expand( $D, '');
?></h2>
<?php
$dirlist = array();
$dir = dir( ($D!='') ? "$D" : ".");
while (($entry = $dir->read()) !== FALSE)
{
if (defined('SHOW_DIR') && is_dir($D.$entry))
{
if (($entry != ".") && !(($entry == "..") && ($D == ""))) // not (this or root_parent)
if (defined('SHOW_PREVIEWS_DIR') || ($entry != substr( PREVIEWS_DIR, 0, -1))) // not (dont_show_preview_dir and is_preview_dir)
$dirlist[] = array( 'name' => $entry, 'size' => DIR_SIZE, 'time' => filemtime($D.$entry));
}
if (is_file($D.$entry))
{
if (is_allowed_ext( strrchr( $entry, '.')))
if (defined('SHOW_THE_SCRIPT') || ($entry != SELFNAME) || ($D != "")) // show_this_script or is_not_this_script
$dirlist[] = array( 'name' => $entry, 'size' => filesize($D.$entry), 'time' => filemtime($D.$entry));
}
}
$dir->close();
print_head();
foreach ($dirlist as $entry)
{
if ($entry['size'] == DIR_SIZE)
{
$link = SELFNAME.'?G=' . (empty($G) ? '0' : '2');
if ($entry['name'] == "..")
{
if ($PARENT != '')
$link .= '&D=' . rawurlencode( substr( $PARENT, 0, -1));
}
else
{
$link .= '&D=' . rawurlencode( $D.$entry['name']);
}
print_dir( $entry, $link);
}
}
foreach ($dirlist as $entry)
{
if ($entry['size'] != DIR_SIZE)
{
$link = $D . rawurlencode( $entry['name']);
print_file( $entry, $link);
}
}
print_foot();
echo "-<br><i><small>".gmdate("D, d M Y H:i:s").' UTC from <a href="http://'.$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].'" title="'.gethostbyname($_SERVER['SERVER_NAME']).'">'.$_SERVER['SERVER_NAME']."</a> port ".$_SERVER['SERVER_PORT']."</small></i>\n";
?>
</body>
</html>
<?php
function dir_expand ($dir, $base)
{
global $G;
if ($dir != '')
{
$dl = strcspn( $dir, '/\\');
$ds = substr( $dir, 0, $dl);
$bs = ($base!='') ? $base.'/'.$ds : $ds;
$return .= '<a href="'.SELFNAME.'?G='.(empty($G) ? '0' : '2').'&D='.rawurlencode($bs).'">'.$ds.'</a>';
$ts = substr( $dir, $dl);
$sl = strspn( $ts, '/\\');
$return .= substr( $ts, 0, $sl);
$return .= dir_expand( substr( $ts, $sl), $bs);
}
return $return;
}
function is_allowed_ext ($ext)
{
$return = TRUE;
if (!empty($ext))
{
$ext .= '.';
if (defined('ALLOWED_EXT'))
if (stristr( ALLOWED_EXT, $ext) == FALSE)
$return = FALSE;
if (defined('DENIED_EXT'))
if (stristr( DENIED_EXT, $ext) != FALSE)
$return = FALSE;
}
else
{
if (defined('ALLOWED_EXT'))
$return = FALSE;
}
return $return;
}
function size_format ($num)
{
if (is_int($num))
$num = ceil($num/1024).'K';
return $num;
}
function find_icon ($entry)
{
$exts = explode( ',', PREVIEWS_EXT);
foreach ($exts as $ext)
{
if (is_file( PREVIEWS_PATH.$entry['name'].'.'.$ext))
return PREVIEWS_PATH.$entry['name'].'.'.$ext;
}
foreach ($exts as $ext)
{
if (is_file( DEFAULT_PREVIEWS_DIR.$entry['name'].'.'.$ext))
return DEFAULT_PREVIEWS_DIR.$entry['name'].'.'.$ext;
}
if (($entry_ext = strrchr( $entry['name'], '.')) != '')
{
$efn = DEFAULTS_EXT_PREFIX.strtolower($entry_ext);
foreach ($exts as $ext)
if (is_file( DEFAULTS_PATH.$efn.'.'.$ext))
return DEFAULTS_PATH.$efn.'.'.$ext;
foreach ($exts as $ext)
if (is_file( DEFAULT_DEFAULTS_DIR.$efn.'.'.$ext))
return DEFAULT_DEFAULTS_DIR.$efn.'.'.$ext;
}
$dfn = ($entry['size'] == DIR_SIZE) ? DEFAULT_DIR_ICON : DEFAULT_FILE_ICON;
foreach ($exts as $ext)
{
if (is_file( DEFAULTS_PATH.$dfn.'.'.$ext))
return DEFAULTS_PATH.$dfn.'.'.$ext;
}
foreach ($exts as $ext)
{
if (is_file( DEFAULT_DEFAULTS_DIR.$dfn.'.'.$ext))
return DEFAULT_DEFAULTS_DIR.$dfn.'.'.$ext;
}
return NULL;
}
function print_dir ($entry, $link)
{
global $G;
if (empty($G))
{
print_list_item( '['.$entry['name'].']', $link, $entry['time'], DIR_SIZE);
}
else
{
print_table_item( $entry['name'], $link, find_icon( $entry), DIR_SIZE);
}
}
function print_file ($entry, $link)
{
global $G;
if (empty($G))
{
print_list_item( $entry['name'], $link, $entry['time'], size_format( $entry['size']));
}
else
{
print_table_item( $entry['name'], $link, find_icon( $entry));
}
}
function print_head ()
{
global $G;
if (empty($G))
{
echo '<pre>'."\n";
}
else
{
echo '<br>'."\n".'<center>'."\n".'<table border="0" cellspacing="0" cellpadding="16">'."\n"; // print_table_head()
}
}
function print_foot ()
{
global $G;
if (empty($G))
{
echo '</pre>'."\n";
}
else
{
echo '</table>'."\n".'</center>'."\n".'<br>'."\n"; // print_table_foot()
}
}
function print_list_item ($name, $link, $time, $size = NULL)
{
$item_name = (strlen($name) <= CM_ITEM_NAME_MAX) ? $name : substr( $name, 0, CM_ITEM_NAME_MAX-3) . '..>';
echo
'<a href="'.$link.'">' . htmlspecialchars($item_name) . '</a>' .
str_pad( '', CM_ITEM_NAME_MAX - strlen($item_name)) .
htmlspecialchars( str_pad( $size, 10, ' ', STR_PAD_LEFT)) . "\t" .
htmlspecialchars( date( DATE_FORMAT, $time)) . "\n";
}
function print_table_item ($name, $link, $icon, $alt = "")
{
static $col=1, $row=1;
$item_name = (strlen($name) <= VM_ITEM_NAME_MAX) ? $name : substr( $name, 0, VM_ITEM_NAME_MAX-3) . '..>';
if ($col==1)
{
echo '<tr align="center" valign="middle">'."\n";
}
echo
'<td><a href="'.$link.'"><img src="'.$icon.'" border="0" alt="'.$alt.'"><samp><br>' .
htmlspecialchars($item_name) . '</samp></a></td>' . "\n";
if (++$col > VM_TABLE_COLS)
{
$col=1; ++$row;
echo '</tr>'."\n";
}
}
?>