Page 1 of 1

clickable directory listing in HTML

Posted: Thu Aug 21, 2003 10:32 am
by mallu
Hi there:

Here is the code for listing the directory - but would like to expand each directory only when clicked

Any help on this will be much aprreciated
Thanks

Biju

<?

function directory_tree($address){

$exts = array("vbs", "js");

$dir = opendir($address);
if(!$dir){
return 0;
}
print "<ul>\n";
while($entry = readdir($dir)){
if(is_dir("$address/$entry") && ($entry != ".." && $entry !="." && $entry !=='icons' && $entry !=='hidden')){
print "<li><a href='$_SERVER[PHP_SELF]?expand=$address/$entry'>$entry</a>\n";
directory_tree("$address/$entry");
}else{
for ($i = 0; $i < count($exts); $i++){
if (eregi("\.". $exts[$i] ."$", $entry)){
print "<li><a href=/$address/$entry'>$entry</a>";
}
}
}
}
print "</ul>";
}



directory_tree("/pinoc/rsrc2/scripts");

?>

Posted: Thu Aug 21, 2003 11:07 am
by JayBird
Copy the following code into a file called dir.php and run

Code: Select all

<html>
    <title>Directory Browser</title>
    <font face="Arial">
    <?php 
    $dirmsg = "Directory of";
    $dir = $_GET['dir'];
    $count = 0;
    if (eregi("\..", $dir)) {
    $dir = ".";
    }
    if ($dir !== "."){
    $prevdir = substr($dir, 0, strpos($dir, "/"));
    if ($prevdir == "") {
    $prevdir = ".";
    }} else {
    $prevdir = ".";
    }
    echo "<p><b>$dirmsg /$dir</b><br><hr size="1"><br><a href="dir.php?dir=$prevdir">Parent Directory</a><br>";
    $directory = @opendir("$dir");
    if ($directory == "") {
    print "<p><b>Error Reading Directory</b><p>";
    } else {
    while( $file = readdir( $directory ) )
    {
    $file_array[] = $file;
    }
    echo"<ul>";
    if ($dir == ".") {
    $dir = "";
    } else {
    $dir = "$dir/";
    }
    foreach( $file_array as $file )
    {
    if( $file == ".." || $file == "." )
    {
    continue;
    }
    if (eregi("\.", $file)) {
    echo"<li><a href="$dir$file">$file</a></li>";
    } else {
    echo"<li><a href="dir.php?dir=$dir$file">/$file</a></li>";
    }
    $count++; 
    }
    echo"</ul>";
    closedir($directory);	
    }
    echo"<hr size="1">There are <b>$count</b> Files In this Directory<br>";
    ?>
    </font>
    </html>
Hope it works

Mark

Posted: Thu Aug 21, 2003 11:44 am
by mallu
Thanks Mark....

but I needed a funtion that is recursive - like the diplay in windows file explorer - where it shows all the directorys from the root - when a directory is clicked it expands and shows the contents of that directory as well as the contents of the parent directory too - maintaing the directory tree structure. Here is the script that I am trying
any help on this script will be much appreciated

$root = "/pinoc/rsrc2/scripts/";
if ($_GET[expand]) {
$expanded = explode('/',trim($_GET[expand],'/'));

coldata($_GET[expand],1);
coldata($expanded,$level);
}else{
coldata($root,0);
}

function coldata($in_dir,$in_level)
{

$exts = array("vbs", "js");
$temp = explode('/pinoc/rsrc2/scripts/',$in_dir);
print "<ul>";


if (is_dir($in_dir)) {
if ($dh = opendir($in_dir)) {
while (($file = readdir($dh)) !== false){
if ($file !== '.' && $file !== '..' && $file !=='icons' && $file !=='hidden') {
if (is_dir($in_dir ."/". $file)){
$dirs[]=$file;
}else{
for ($i = 0; $i < count($exts); $i++){
if (eregi("\.". $exts[$i] ."$", $file)){
$files[] = $file;
}
}
}
}
}
}
closedir($dh);
}
sort($dirs);



foreach($dirs as $value){
print "<li><a href='$_SERVER[PHP_SELF]?expand=$in_dir$value/'>$value</a>\n";
if ($value == $expanded[$in_level]){
print "<tr>test</tr>";
}

}

//print count($files);

if (count($files) > 0){
foreach ($files as $file){
print "<li>$file";
}


}//print "</ul>";

}

?>

Posted: Thu Aug 21, 2003 2:48 pm
by m3rajk
this is meant to get certain picture file types out of subdirectories. if you modify it, you can do what you want. but if i modify it for you you wont learn what's happening, so i'm going to give it to you as is, and let you play with modifying it to learn what's going on.

Code: Select all

## set the directory
$dir = $_GET['dir'];

## get the contents
$jpgs=array(); $pngs=array(); $gifs=array();
$d=opendir($dir) or die($php_errormsg);
while(false !==($f=readdir($d))){
  $here=getcwd(); $pospics=$here . '/' . $dir . $f;
  if(is_file($pospics)){
    if((preg_match('/\.jpg$/', $f))||(preg_match('/\.jpe$/', $f))||(preg_match('/\.jpeg$/', $f))){
      $jpgs[] = $f;
    }elseif(preg_match('/\.png$/', $f)){
      $pngs[] = $f;
    }elseif(preg_match('/\.gif$/', $f)){
      $gifs[] = $f;
    }
  }
}
sort($jpgs); sort($pngs); sort($gifs);
if you'd like me to explain what hte lines are doing let me know

Posted: Tue Aug 26, 2003 1:26 pm
by mallu
Hi there:

Finally I figured out a way to do it...

Thanks Mark..

Here is the code



print ("<table width=100% border=0 cellspacing=0 cellpadding=25>");
print (" <tr>");
print (" <td><table border=0 cellspacing=0 cellpadding=0 > \n");

$test = explode('/',$_GET[expand]);
directory_tree("/pinoc/rsrc2/scripts/",0);


function directory_tree($in_dir,$level){

global $test;

$root = "/pinoc/rsrc2/scripts/";
ereg ('^/pinoc/rsrc2/scripts/(.*)$', $in_dir, $regs);

$exts = array("vbs", "js","preset");

if (is_dir($in_dir)) {
if ($dh = opendir($in_dir)) {
while (($file = readdir($dh)) !== false){
if ($file != '.' && $file != '..' && $file != 'icons' && $file != 'hidden') {
if (is_dir ($in_dir ."/". $file)){
$dirs[]=$file;
}
else{
for ($i = 0; $i < count($exts); $i++){
if (eregi("\.". $exts[$i] ."$", $file)){

$files[] = $file;
}
}
}
}
}
closedir ($dh);
}
}
sort ($dirs);

ereg ('^/pinoc/rsrc2/scripts/(.*)$', $in_dir, $regs);
$spacer = '';

for ($x = 0; $x < substr_count ($regs[1], '/'); $x++) {
$spacer .= "<td>&nbsp;</td>";
}
//print $_get[expand];

foreach($dirs as $value) {
if ($_GET[expand]=="$regs[1]$value/"){
print "<tr>$spacer<td><a href='$_SERVER[PHP_SELF]?expand=$regs[1]'>" . str_replace("_"," ",$value). "</a></td><tr>\n";
}
else{
print "<tr>$spacer<td><a href='$_SERVER[PHP_SELF]?expand=$regs[1]$value/'>" . str_replace("_"," ",$value). "</a></td><tr>\n";
}

if ($value == $test[$level]){
directory_tree("$in_dir" . $test[$level] . "/",$level+1);
}
}

sort($files);
if (count($files) > 0 ){
foreach($files as $file){
ereg('\.(.*)$',$file, $ext);
switch($ext[0]){
case '.vbs':
$extico='vb.png';
break;

case '.js':
$extico = 'js.png';
break;

case '.preset':
$extico = 'p.png';
break;

default:
$extico = 'qm.png';
break;
}
//print "<tr>$spacer<td><img src='vb.png' border='0' align='right'</td><td><a href=" . mangler($in_dir).$file . ">" . str_replace("_"," ",$file) . "</a></td><tr>\n";
print "<tr>$spacer<td><img src=" . $extico . " border='0'>&nbsp;<a href=" . mangler($in_dir).$file . ">" . str_replace("_"," ",$file) . "</a></td><tr>\n";
}
}
}


function mangler ($path)
{
if (preg_match ('/windows/i', $_SERVER[HTTP_USER_AGENT])) {
$path = preg_replace ('|^/pinoc/rsrc2/|', 'file://sgi3200/pinocchio/rsrc2/', $path);
}
return ($path);
}