<?php
$dir = ".";
$directories = array();
$files = array();
$dir = (substr($dir, -1) == '/') ? substr($dir, 0, -1) : $dir;
if(is_dir($dir)){
if($handle = opendir($dir)){
while(false !== ($file = readdir($handle))){
if($file != "." && $file != ".."){
if(is_dir($dir.'/'.$file)){
$directories[$file] = filemtime($dir.'/'.$file);
} else {
$files[$file] = filemtime($dir.'/'.$file);
}
}
}
closedir($handle);
} else {
die('Could not open directory.');
}
} else {
die('Invalid directory.');
}
if($_GET['sort'] == 'alpha'){
if($_GET['mode'] == 'desc'){
krsort($files);
$highlight = 'alpha_desc';
} else {
ksort($files);
$highlight = 'alpha_asc';
}
} else {
if($_GET['mode'] == 'asc'){
asort($files, SORT_NUMERIC);
$highlight = 'date_asc';
} else {
arsort($files, SORT_NUMERIC);
$highlight = 'date_desc';
}
}
$sort_alpha_asc = ($highlight == 'alpha_asc') ? '<b>Asc</b>' : '<a href="?sort=alpha&mode=asc">Asc</a>';
$sort_alpha_desc = ($highlight == 'alpha_desc') ? '<b>Desc</b>' : '<a href="?sort=alpha&mode=desc">Desc</a>';
$sort_date_asc = ($highlight == 'date_asc') ? '<b>Asc</b>' : '<a href="?sort=date&mode=asc">Asc</a>';
$sort_date_desc = ($highlight == 'date_desc') ? '<b>Desc</b>' : '<a href="?sort=date&mode=desc">Desc</a>';
echo "Sort by: Date- $sort_date_asc | $sort_date_desc; Name- $sort_alpha_asc | $sort_alpha_desc<br />\n<br />\n";
echo "<table border=\"0\">\n<tr><td><u>File</u></td><td width=\"25\"></td><td><u>Size</u></td><td width=\"25\"></td><td><u>Last Modified</u></td></tr>\n";
foreach($files as $file => $timestamp){
echo "<tr><td><a href=\"$dir/$file\">$file</a></td><td></td><td>";
$filesize = filesize($dir.'/'.$file);
if($filesize >= 1048576){
echo round($filesize / 1048576, 1).'MB';
} else {
echo round($filesize / 1024, 1).'kb';
}
echo '</td><td></td><td>'.date('M d Y H:i:s', $timestamp)."</td></tr>\n";
}
echo '</table>';
?>