Directory Listing with Pagination

This script is a directory lister that shows whatever files you want using the glob PHP function. It can be configured to show as many or as little files per page are you want. You can also exclude certain directories of your choice. The pagination part perhaps needs a little attention but you should be able to work thing out OK.



<?php
    
##############
# EDIT THESE #
##############
//~ amount of links shown    
$limit 10;  
//~ must exist within weboot (or doc-root itself)
$base  rtrim($_SERVER['DOCUMENT_ROOT'], '/') .'/'
//~ disallow browsing of certain directories (lowercase!!)
$forbidden_files = array(
            
'private',
            
'admin',
            
'etc'); 
//~ what files do you want to look for?
$glob_what = array('jpg','JPG','gif','GIF','png','PNG');
//~ location of images used in CSS 
$icons   '/icons/'
##############
# END EDIT   #
##############
    
//~ Make $forbidden_files relative
foreach ( $forbidden_files as $forbid ) {
    
$forbidden []= strtolower($base) . $forbid .'/';
}
    
//~ find out what you are searching for
foreach ($glob_what as $ext) {
   
$glob_this []= '*.'$ext
}
    
//~ make it into a correctly formatted string
$glob_this implode(',',$glob_this);
    
//~ stripos for php4
if (!function_exists("stripos")) {
    function 
stripos($haystack$needle$offset=0) {
        return 
strpos(strtolower($haystack), strtolower($needle), $offset);
    }
}
    
$this_dir = isset($_GET['dir']) ? $base htmlentities($_GET['dir']) : $base;
    
$dir_listing '';
$header '<h1><a href="'htmlentities($_SERVER['PHP_SELF']) 
.
'">Directories</a></h1>'."\n";
    
//~ Start listing directories
if ( is_dir($this_dir) AND !in_array(strtolower($this_dir), $forbidden) ){
    
$up_explode explode ('/'$this_dir);
    
$up_dir     array_diff($up_explode, array(''));
    
$up_explode array_pop($up_dir);
    
    
$dir_list   glob($this_dir ."*",GLOB_ONLYDIR);
    
$file_list  glob($this_dir ."{"$glob_this ."}",GLOB_BRACE);
    
$pages      array_chunk($file_list$limit);
    
$header    .= '<h2>'$up_explode .'</h2>'."\n";
    
    
$this_page  = ( isset($_GET['page']) 
        AND 
is_numeric($_GET['page']) 
        AND 
count($pages) >= $_GET['page'] ) ? 
        
$_GET['page'] : 0;
    
    
//~ Actual listing
            
        
if (  count($dir_list) != ) {
            
$dir_listing .= '<ul id="dirs">'."\n";
        foreach (
$dir_list as $dir_key => $dir_val) {
            if ( !
in_array(strtolower($dir_val) .'/'$forbidden) ){
            
$dir_listing .= '<li class="dir"><a href="'
            
htmlentities($_SERVER['PHP_SELF']) 
            .
'?dir='
            
str_replace($base''$dir_val) .'/">'
            
str_replace($base''$dir_val).'</a></li>'."\n";
            }
        }
            
$dir_listing .= '</ul>'."\n";
        } else {
            
$dir_listing .= '<p>No directories here</p>'."\n";
        }
        
    
// LINK TO PREVIOUS DIR
    
if ( $this_dir != $base ) {
           
        
$up_link '';
        foreach (
$up_dir as $u_key) {
            if ( 
strpos($base$u_key) === FALSE ) { 
            
$up_link .= $u_key .'/';
            }
        }
        
$up_start = ($base{0} == '/') ? '/' '';
        
$nav_link htmlentities($_SERVER['PHP_SELF'])  .'?dir='
                                
$up_start $up_link;
        
$dir_listing .= '<p id="uplink"><a href="'$nav_link 
                                   
.'">Up</a></p>'."\n";
    }
    
// END LINK TO PREVIOUS DIR
    
$dir_listing .= '<div class="hr" />&nbsp;</div>'."\n";
    
$file_listing '<h1>Files</h1>'."\n";
    
if ( 
count($file_list) != ) {
    
    
// FILE DISPLAY STUFF
    
$file_listing .= '<ul id="files">'."\n";
    foreach (
$pages[$this_page] as $key => $val) {
        
$link_val '/'str_replace($base''$val);
        
$class    pathinfo($val);
        
$file_listing .= '<li class="'
        
strtolower($class['extension']) .'"><a href="'$link_val .'">'
        
basename($val) .'</a> ('filesize($val) .' bytes)</li>'."\n";
    }
    
$file_listing .= '</ul>'."\n";
    
// END FILE DISPLAY STUFF
    
    // PAGINATION
    
$next '';
    
$back = ( $this_dir != $base ) ? '<a href="'$nav_link .'">Prev</a>' '';
    
    if ( 
array_key_exists( ($this_page 1), $pages ) ) {
        
$next '<a href="'htmlentities($_SERVER['PHP_SELF'])  .'?dir='
        
urldecode(str_replace($base''$this_dir)) 
        .
'&amp;page='. ($this_page 1) .'">Next</a>';
    }
    if ( 
array_key_exists( ($this_page 1), $pages ) ) {
        
$back '<a href="'htmlentities($_SERVER['PHP_SELF'])  .'?dir='
        
urldecode(str_replace($base''$this_dir)) 
        .
'&amp;page='. ($this_page ) .'">Prev</a>';
    }
    
if (
$next != '' AND $back != ''){
    
$file_listing .= '<p><span class="back">'$back .'</span> | 
<span class="next">'
$next .'</span></p>'."\n";
} elseif (
$next != '' AND $back == ''){
    
$file_listing .= '<p><span class="next">'$next .'</span></p>'."\n";
} elseif (
$next == '' AND $back != ''){
    
$file_listing .= '<p><span class="back">'$back .'</span></p>'."\n";
}
    
$file_listing .= '<p>'count($file_list) .' '. ( count($file_list) == 
    
'file' 'files' ) .' in this directory.</p>'."\n";
    
// END PAGINATION
        
} else {
    
$file_listing .= '<p>No files to display</p>'."\n";
}
    
} else {
$dir_listing '<p class="warning">Sorry, directory ('
                            
$this_dir .') does not exist</p>'
$file_listing '';
}
    
// DEBUG
$footer '<div class="hr">&nbsp;</div>
<pre>'
."\n";
$footer .= print_r($_GET1);
$footer .=  '</pre>'."\n";
// END DEBUG;
    
echo '<?xml version="1.0" encoding="utf-8"?>'."\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Directory Listings</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
html,body {color: #000; background: #fff; font-size: 80%; 
font-family: 'verdana', tahoma, sans-serif;}
h2 { background: url(<?php echo $icons ?>folder.open.gif) no-repeat center left;
padding-left: 30px; }
#uplink { background: url(<?php echo $icons ?>up.gif) no-repeat center left;
padding-left: 30px; }
.back { background: url(<?php echo $icons ?>left.gif) no-repeat center left;
padding-left: 30px; }
.next { background: url(<?php echo $icons ?>right.gif) no-repeat center right;
padding-right: 30px; }
#dirs li {list-style-type: square;}
#files li {list-style-type: circle;}
li.dir {list-style-image: url(<?php echo $icons ?>folder.gif);}
li.jpg {list-style-image: url(<?php echo $icons ?>image1.gif);}
li.gif {list-style-image: url(<?php echo $icons ?>image2.gif);}
li.png {list-style-image: url(<?php echo $icons ?>image3.gif);}
li.pdf {list-style-image: url(<?php echo $icons ?>pdf.gif);}
a {color: #454545;}
.hr { line-height: 1px; background: #454545;}
</style>
</head>
<body>
<?php
echo $header$dir_listing$file_listing$footer;
?>
</body>
</html>
Syndicate content