<?php 
 
require_once(dirname(__FILE__).'/hierarchy.class.php'); 
 
$list = get_directory_list(); 
$list_hierarchicaly = hierarchy::print_list($list); 
 
header('Content-type: text/html; charset=UTF-8'); 
echo <<<HTML 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" > 
<title>Example</title> 
<link rel="stylesheet" type="text/css" media="all" href="hierarchy.css.php" /> 
<style><!-- 
img { border: 0; } 
a { text-decoration: none; } 
--></style> 
</head> 
<body> 
<h1>Directory List</h1> 
<div style="border: 1px solid red; padding: 0 1em;"> 
{$list_hierarchicaly} 
</div> 
<p>You can print a directory content hierarchicaly.</p> 
</body> 
</html> 
HTML; 
 
 
/** 
 * Example of function that create a list dinamicaly. 
 * 
 * @return array[string => (bool || type)] 
 */ 
function get_directory_list() { 
 
    // Save opened items in session 
    session_start(); 
    if (!isset($_SESSION['opened'])) { 
        $_SESSION['opened'] = array(); 
    } 
 
    $dirname = dirname(__FILE__); 
    $location = ''; 
 
    // If open action was requested 
    if (isset($_GET['open'])) { 
        $open = urldecode($_GET['open']); 
        $open = realpath($dirname.'/'.str_replace('.', '', $open)); 
        if (is_dir($open)) { 
            if (isset($_SESSION['opened'][$open])) { 
                unset($_SESSION['opened'][$open]); 
            } else { 
                $_SESSION['opened'][$open] = true; 
            } 
        } 
    } 
 
    return get_directory_list_recursion($dirname, $location); 
} 
 
 
/** 
 * Function recursion. 
 * 
 * @param string $dirname Directory to be opened 
 * @param string $location Short directory name 
 * @return array[string => (bool || type)] 
 */ 
function get_directory_list_recursion($dirname, $location) { 
    $dirname = realpath($dirname); 
    $dir = scandir($dirname); 
    $list = array(); 
    $has_content = false; 
    foreach ($dir as $item) { 
        if ($item == '.' || $item == '..') { 
            continue; 
        } 
        $has_content = true; 
 
        // Directory 
        if (is_dir($dirname.'/'.$item)) { 
            $open = urlencode($location.'/'.$item); 
 
            // If it is open 
            if (isset($_SESSION['opened'][$dirname.'/'.$item])) { 
                $img = '<img src="imgs/dir_opened.gif" alt="opened directory" />'; 
                $content = '<a href="example_directory.php?open='.$open.'">'.$img.' '.$item.'</a>'; 
                $list[$content] = get_directory_list_recursion($dirname.'/'.$item, $location.'/'.$item); 
 
            // If it is close 
            } else { 
                $img = '<img src="imgs/dir_closed.gif" alt="closed directory" />'; 
                $content = '<a href="example_directory.php?open='.$open.'">'.$img.' '.$item.'</a>'; 
                $list[$content] = false; 
            } 
 
        // File 
        } else { 
            $list[$item] = false; 
        } 
    } 
 
    if (!$has_content) { 
        $list['empty'] = false; 
    } 
    return $list; 
} 
 
 |