" . $path . " is not a valid path.");
}
//
// Get the size in bytes of a folder
//
function foldersize($path) {
$size = 0;
if($handle = @opendir($path)){
while(($file = readdir($handle)) !== false) {
if(is_file($path."/".$file)){
$size += filesize($path."/".$file);
}
if(is_dir($path."/".$file)){
if($file != "." && $file != "..") {
$size += foldersize($path."/".$file);
}
}
}
}
return $size;
}
//
// This function returns the file size of a specified $file.
//
function format_bytes($size, $precision=0) {
$sizes = array('YB', 'ZB', 'EB', 'PB', 'TB', 'GB', 'MB', 'KB', 'B');
$total = count($sizes);
while($total-- && $size > 1024) $size /= 1024;
return sprintf('%.'.$precision.'f', $size).$sizes[$total];
}
//
// This function returns the mime type of $file.
//
function get_file_type($file) {
global $image_types, $movie_types;
$pos = strrpos($file, ".");
if ($pos === false) {
return "Unknown File";
}
$ext = rtrim(substr($file, $pos+1), "~");
if(in_array($ext, $image_types)) {
$type = "Image";
} elseif(in_array($ext, $movie_types)) {
$type = "Video";
} else {
$type = "File";
}
return(strtoupper($ext) . " " . $type);
}
// Print the heading stuff
$vpath = ($path != "./")?$path:"";
print "
Index of /" .$vpath. "
Index of /" . $vpath ."
";
// Get all of the folders and files.
$folderlist = array();
$filelist = array();
if($handle = @opendir($path)) {
while(($item = readdir($handle)) !== false) {
if(is_dir($path.'/'.$item) and $item != '.' and $item != '..') {
$folderlist[] = array(
'name' => $item,
'size' => (($calculate_folder_size)?foldersize($path.'/'.$item):0),
'modtime'=> filemtime($path.'/'.$item),
'file_type' => "Directory"
);
}
elseif(is_file($path.'/'.$item)) {
if(!$show_hidden_files) {
if(substr($item, 0, 1) == "." or substr($item, -1) == "~") {
continue;
}
}
$filelist[] = array(
'name'=> $item,
'size'=> filesize($path.'/'.$item),
'modtime'=> filemtime($path.'/'.$item),
'file_type' => get_file_type($path.'/'.$item)
);
}
}
fclose($handle);
}
if(!isset($_GET['sort'])) {
$_GET['sort'] = 'name';
}
// Figure out what to sort files by
$file_order_by = array();
foreach ($filelist as $key=>$row) {
$file_order_by[$key] = $row[$_GET['sort']];
}
// Figure out what to sort folders by
$folder_order_by = array();
foreach ($folderlist as $key=>$row) {
$folder_order_by[$key] = $row[$_GET['sort']];
}
// Order the files and folders
if($_GET['order']) {
array_multisort($folder_order_by, SORT_DESC, $folderlist);
array_multisort($file_order_by, SORT_DESC, $filelist);
} else {
array_multisort($folder_order_by, SORT_ASC, $folderlist);
array_multisort($file_order_by, SORT_ASC, $filelist);
$order = "&order=desc";
}
// Show sort methods
print "";
$sort_methods = array();
$sort_methods['name'] = "Name";
$sort_methods['modtime'] = "Last Modified";
$sort_methods['size'] = "Size";
$sort_methods['file_type'] = "Type";
foreach($sort_methods as $key=>$item) {
if($_GET['sort'] == $key) {
print "$item | ";
} else {
print "$item | ";
}
}
print "
";
// Parent directory link
if($path != "./") {
print "Parent Directory/ | ";
print " | ";
print " | ";
print "Directory |
";
}
// Print folder information
foreach($folderlist as $folder) {
print "" .htmlentities($folder['name']). "/ | ";
print "" . date('Y-M-d H:m:s', $folder['modtime']) . " | ";
print "" . (($calculate_folder_size)?format_bytes($folder['size'], 2):'--') . " | ";
print "" . $folder['file_type'] . " |
";
}
// This simply creates an extra line for file/folder seperation
print " |
";
// Print file information
foreach($filelist as $file) {
print "" .htmlentities($file['name']). " | ";
print "" . date('Y-M-d H:m:s', $file['modtime']) . " | ";
print "" . format_bytes($file['size'],2) . " | ";
print "" . $file['file_type'] . " |
";
}
// Print ending stuff
print "
Lighttpd Enhanced Directory Listing Script
";
/* -------------------------------------------------------------------------------- *\
I hope you enjoyed my take on the enhanced directory listing script!
If you have any questions, feel free to contact me.
Regards,
Evan Fosmark < me@evanfosmark.com >
\* -------------------------------------------------------------------------------- */
?>