'lname', 'sort'=>'asc'), // ... this sets the initial sort "column" and order ... array('key'=>'size', 'sort'=>'asc') // ... for items with the same initial sort value, sort this way. ); // Files you want to hide form the listing $ignore_list = array('listr.css','listr-favicon.png','.DS_Store','robots.txt'); /***[ DIRECTORY LOGIC ]***/ // Get this folder and files name. $this_script = basename(__FILE__); $this_folder = str_replace('/'.$this_script, '', $_SERVER['SCRIPT_NAME']); $this_domain = $_SERVER['SERVER_NAME']; $dir_name = explode("/", $this_folder); //$dir_name = explode("/",dirname($_SERVER['REQUEST_URI'])) //$dir_path = explode("/", $this_folder); // Declare vars used beyond this point. $file_list = array(); $folder_list = array(); $total_size = 0; $filetype = array( 'doc' => array('doc','docs','pdf','pages','key','numbers','xls','ppt'), 'text' => array('txt', 'rtf', 'text', 'nfo', 'md', 'markdown'), 'audio' => array('aac', 'mp3', 'wav', 'wma', 'm4p'), 'image' => array('ai', 'bmp', 'eps', 'gif', 'ico', 'jpg', 'jpeg', 'png', 'psd', 'psp', 'raw', 'tga', 'tif', 'tiff','icns'), 'video' => array('mv4', 'bup', 'mkv', 'ifo', 'flv', 'vob', '3g2', 'bik', 'xvid', 'divx', 'wmv', 'avi', '3gp', 'mp4', 'mov', '3gpp', '3gp2', 'swf', 'mpg', 'mpeg'), 'archive' => array('7z', 'dmg', 'rar', 'sit', 'zip', 'bzip', 'gz', 'tar','pkg','safariextz','bz2'), 'app' => array('ipa', 'exe', 'app'), 'script' => array('js', 'html', 'htm', 'xhtml', 'jsp', 'asp', 'aspx', 'php', 'xml', 'css', 'plist') ); // Open the current directory... if ($handle = opendir('.')) { // ...start scanning through it. while (false !== ($file = readdir($handle))) { // Make sure we don't list this folder, file or their links. if ($file != "." && $file != ".." && $file != $this_script && !in_array($file, $ignore_list)) { // Get file info. $stat = stat($file); // ... slow, but faster than using filemtime() & filesize() instead. $info = pathinfo($file); // Organize file info. $item['name'] = $info['filename']; $item['lname'] = strtolower($info['filename']); $item['ext'] = $info['extension']; $item['lext'] = strtolower($info['extension']); if($info['extension'] == '') $item['ext'] = '.'; if(in_array($item[lext], $filetype['doc'])){ $item['class'] = 'type-document'; }elseif(in_array($item[lext], $filetype['text'])){ $item['class'] = 'type-text'; }elseif(in_array($item[lext], $filetype['audio'])){ $item['class'] = 'type-audio'; }elseif(in_array($item[lext], $filetype['image'])){ $item['class'] = 'type-image'; }elseif(in_array($item[lext], $filetype['video'])){ $item['class'] = 'type-video'; }elseif(in_array($item[lext], $filetype['archive'])){ $item['class'] = 'type-archive'; }elseif(in_array($item[lext], $filetype['app'])){ $item['class'] = 'type-app'; }elseif(in_array($item[lext], $filetype['script'])){ $item['class'] = 'type-script'; }else{ $item['class'] = 'type-generic'; } $item['bytes'] = $stat['size']; $item['size'] = bytes_to_string($stat['size'], 2); $item['mtime'] = $stat['mtime']; // Add files to the file list... if($info['extension'] != ''){ array_push($file_list, $item); } // ...and folders to the folder list. else{ array_push($folder_list, $item); } // Clear stat() cache to free up memory (not really needed). clearstatcache(); // Add this items file size to this folders total size $total_size += $item['bytes']; } } // Close the directory when finished. closedir($handle); } // Sort folder list. if($folder_list) $folder_list = php_multisort($folder_list, $sort); // Sort file list. if($file_list) $file_list = php_multisort($file_list, $sort); // Calculate the total folder size (fix: total size cannont display while there is no folder inside the directory) if($file_list && $folder_list || $file_list) $total_size = bytes_to_string($total_size, 2); $total_folders = count($folder_list); $total_files = count($file_list); if ($total_folders > 0){ if ($total_folders > 1){ $funit = 'folders'; }else{ $funit = 'folder'; } $contained = $total_folders.' '.$funit; } if ($total_files > 0){ if($total_files > 1){ $iunit = 'items'; }else{ $iunit = 'item'; } if (isset($contained)){ $contained .= ' & '.$total_files.' '.$iunit; }else{ $contained = $total_files.' '.$iunit; } } /***[ FUNCTIONS ]***/ /** * http://us.php.net/manual/en/function.array-multisort.php#83117 */ function php_multisort($data,$keys) { foreach ($data as $key => $row) { foreach ($keys as $k) { $cols[$k['key']][$key] = $row[$k['key']]; } } $idkeys = array_keys($data); $i=0; foreach ($keys as $k) { if($i>0){$sort.=',';} $sort.='$cols['.$k['key'].']'; if($k['sort']){$sort.=',SORT_'.strtoupper($k['sort']);} if($k['type']){$sort.=',SORT_'.strtoupper($k['type']);} $i++; } $sort .= ',$idkeys'; $sort = 'array_multisort('.$sort.');'; eval($sort); foreach($idkeys as $idkey) { $result[$idkey]=$data[$idkey]; } return $result; } /** * @ http://us3.php.net/manual/en/function.filesize.php#84652 */ function bytes_to_string($size, $precision = 0) { $sizes = array('YB', 'ZB', 'EB', 'PB', 'TB', 'GB', 'MB', 'KB', 'Bytes'); $total = count($sizes); while($total-- && $size > 1024) $size /= 1024; $return['num'] = round($size, $precision); $return['str'] = $sizes[$total]; return $return; } /** * @ http://us.php.net/manual/en/function.time.php#71342 */ function time_ago($timestamp, $recursive = 0) { $current_time = time(); $difference = $current_time - $timestamp; $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); $lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600); for ($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) <= 1); $val--); if ($val < 0) $val = 0; $new_time = $current_time - ($difference % $lengths[$val]); $number = floor($number); if($number != 1) { $periods[$val] .= "s"; } $text = sprintf("%d %s ", $number, $periods[$val]); if (($recursive == 1) && ($val >= 1) && (($current_time - $new_time) > 0)) { $text .= time_ago($new_time); } return $text; } /***[ HTML TEMPLATE ]***/ ?>