' . $units[$pow] . ""; } function count_dir_files( $dir) { $fi = new FilesystemIterator(__DIR__ . "/" . $dir, FilesystemIterator::SKIP_DOTS); return iterator_count($fi); } function get_directory_size($path) { $bytestotal = 0; $path = realpath($path); if($path!==false && $path!='' && file_exists($path)) { foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object) { $bytestotal += $object->getSize(); } } return display_size($bytestotal); } // SHOW THE MEDIA BLOCK function display_block( $file ) { global $ignore_file_list, $ignore_ext_list, $force_download; $file_ext = ext($file); if(is_dir($file)) $file_ext = "dir"; if(in_array($file, $ignore_file_list)) return; if(in_array($file_ext, $ignore_ext_list)) return; $download_att = ($force_download AND $file_ext != "dir" ) ? " download=\"" . htmlEntities(basename($file), ENT_QUOTES) . "\"" : ""; $file_url = htmlEntities(rawurlencode($file), ENT_QUOTES); $rtn = "
".PHP_EOL; $rtn .= "".PHP_EOL; $rtn .= "
".PHP_EOL; $rtn .= "
".PHP_EOL; if ($file_ext === "dir") { $rtn .= "
" . htmlspecialchars(basename($file), ENT_QUOTES) . "
".PHP_EOL; $rtn .= "
" . count_dir_files($file) . " files
".PHP_EOL; $rtn .= "
Size: " . get_directory_size($file) . "
".PHP_EOL; } else { $rtn .= "
" . htmlspecialchars(basename($file), ENT_QUOTES) . "
".PHP_EOL; $rtn .= "
Size: " . display_size(filesize($file)) . "
".PHP_EOL; $rtn .= "
Last modified: " . date("D. F jS, Y - h:ia", filemtime($file)) . "
".PHP_EOL; } $rtn .= "
".PHP_EOL; $rtn .= "
".PHP_EOL; $rtn .= "
".PHP_EOL; return $rtn; } // RECURSIVE FUNCTION TO BUILD THE BLOCKS function build_blocks( $items, $folder ) { global $ignore_file_list, $ignore_ext_list, $sort_by, $toggle_sub_folders, $ignore_empty_folders; $objects = array(); $objects['directories'] = array(); $objects['files'] = array(); foreach($items as $c => $item) { if( $item == ".." OR $item == ".") continue; // IGNORE FILE if(in_array($item, $ignore_file_list)) { continue; } if( $folder !== false ) { $item = "$folder/$item"; } $file_ext = ext($item); // IGNORE EXT if(in_array($file_ext, $ignore_ext_list)) { continue; } // DIRECTORIES if( is_dir($item) ) { $objects['directories'][] = $item; continue; } // FILE DATE $file_time = date("U", filemtime($item)); // FILES if( $item ) { $objects['files'][$file_time . "-" . $item] = $item; } } foreach($objects['directories'] as $c => $file) { $sub_items = (array) scandir( $file ); if( $ignore_empty_folders ) { $has_sub_items = false; foreach( $sub_items as $sub_item ) { $sub_fileExt = ext( $sub_item ); if( $sub_item == ".." OR $sub_item == ".") continue; if(in_array($sub_item, $ignore_file_list)) continue; if(in_array($sub_fileExt, $ignore_ext_list)) continue; $has_sub_items = true; break; } if( $has_sub_items ) echo display_block( $file ); } else { echo display_block( $file ); } if( $toggle_sub_folders ) { if( $sub_items ) { $file_url = htmlEntities(rawurlencode($file), ENT_QUOTES); echo "
"; build_blocks( $sub_items, $file ); echo "
"; } } } // SORT BEFORE LOOP if( $sort_by == "date_asc" ) { ksort($objects['files']); } elseif( $sort_by == "date_desc" ) { krsort($objects['files']); } elseif( $sort_by == "name_asc" ) { natsort($objects['files']); } elseif( $sort_by == "name_desc" ) { arsort($objects['files']); } foreach($objects['files'] as $t => $file) { $fileExt = ext($file); if(in_array($file, $ignore_file_list)) { continue; } if(in_array($fileExt, $ignore_ext_list)) { continue; } echo display_block( $file ); } } // GET THE BLOCKS STARTED, FALSE TO INDICATE MAIN FOLDER $items = scandir( dirname(__FILE__) ); build_blocks( $items, false ); ?>
Free PHP File Directory Script (GitHub)