" . $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 != '..') { if(!$show_hidden_files) { $tmp_item=trim(preg_replace('@\/@','',$item)); if(preg_match("@{$tmp_item}@",$hidden)) continue; } $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; } $tmp_item=trim(preg_replace('@\/@','',$item)); if(preg_match("@{$tmp_item}@",$hidden)) 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 ""; } else { print ""; } } print ""; // Parent directory link if($path != "./") { print ""; print ""; print ""; print ""; } // Print folder information foreach($folderlist as $folder) { print "<"; print ""; print ""; print ""; } // This simply creates an extra line for file/folder seperation print ""; // Print file information foreach($filelist as $file) { print ""; print ""; print ""; print ""; } $base="/home/debian/public_html/"; $uri=$_SERVER['REQUEST_URI']; if(file_exists($base.$uri."README.html")) $readme=file_get_contents($base.$uri."README.html"); else $readme=''; // Print ending stuff print "
$item$item
Parent Directory/  Directory
"; print "" .htmlentities($folder['name']). "/" . date('Y-M-d H:m:s', $folder['modtime']) . "" . (($calculate_folder_size)?format_bytes($folder['size'], 2):'--') . " " . $folder['file_type'] . "
" .htmlentities($file['name']). "" . date('Y-M-d H:m:s', $file['modtime']) . "" . format_bytes($file['size'],2) . " " . $file['file_type'] . "
". $_ENV['SERVER_SOFTWARE'] . "
".$readme." "; /* -------------------------------------------------------------------------------- *\ 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 > \* -------------------------------------------------------------------------------- */ ?>