Folder View Structure IN PHP

Folder View use unordered list for building the tree. You have to construct the unordered list, in order to display the files and sub-folders of particular folder of your server into a tree.

File : Foldertree.php

[wp_ad_camp_1]

<?php
class treeview {

private $files;
private $folder;

function __construct( $path ) {

$files = array();

if( file_exists( $path)) {
if( $path[ strlen( $path ) – 1 ] == ‘/’ )
$this->folder = $path;
else
$this->folder = $path . ‘/’;

$this->dir = opendir( $path );
while(( $file = readdir( $this->dir ) ) != false )
$this->files[] = $file;
closedir( $this->dir );
}
}

function create_tree( ) {

if( count( $this->files ) > 2 ) { /* First 2 entries are . and .. -skip them */
natcasesort( $this->files );
$list = ‘<ul class=”filetree” style=”display: none;”>’;
// Group folders first
foreach( $this->files as $file ) {
if( file_exists( $this->folder . $file ) && $file != ‘.’ && $file != ‘..’ && is_dir( $this->folder . $file )) {
$list .= ‘<li class=”folder collapsed”><a href=”#” rel=”‘ . htmlentities( $this->folder . $file ) . ‘/”>’ . htmlentities( $file ) . ‘</a></li>’;
}
}
// Group all files
foreach( $this->files as $file ) {
if( file_exists( $this->folder . $file ) && $file != ‘.’ && $file != ‘..’ && !is_dir( $this->folder . $file )) {
$ext = preg_replace(‘/^.*\./’, ”, $file);
$list .= ‘<li class=”file ext_’ . $ext . ‘”><a href=”#” rel=”‘ . htmlentities( $this->folder . $file ) . ‘”>’ . htmlentities( $file ) . ‘</a></li>’;
}
}
$list .= ‘</ul>’;
return $list;
}
}
}

$path = urldecode( $_REQUEST[‘dir’] );
$tree = new treeview( $path );
echo $tree->create_tree();
?>

Download Source Code

4236total visits,11visits today