PHP Classes

File: list_dir.php

Recommend this page to a friend!
  Packages of Hans Körber   Phptempt   list_dir.php   Download  
File: list_dir.php
Role: Example script
Content type: text/plain
Description: Recursively print contents of a specified directory
Class: Phptempt
Template engine for PHP and Java
Author: By
Last change:
Date: 23 years ago
Size: 1,896 bytes
 

 

Contents

Class file image Download
<?php

// include API class:
include ("phptempt.php");

function listfolder ($dirpath)
{
    global $t;
    global $con;

    static $count = 0; // debug

    // begin block:
    $t->begin ("dirlist");

    // add value to a placeholer in the block:
    $t->addVal ("FOLDERNAME", $dirpath);

    // open folder; if an error occurres on attempt to open folder,
    // then issue a message:
    //
    $dir = @opendir ($dirpath);
//    echo "dir==$dir<br>"; // debug
    if (is_bool($dir) && !$dir) {
        $t->begin ("entry");
        $t->addVal ("NOACCESS", ++$count);
        $t->end ("entry");
    }

    // otherwise, print contents of folder:
    //
    else {

        while ($file = readdir ($dir)) {

            // ignore . and .. entries:
            if ($file=="." || $file=="..")
                continue;

            $filepath = "$dirpath/$file";

            $t->begin ("entry");

            if (is_dir($filepath)) {
                // recursively list subfolder:
                listfolder ($filepath);
            }
            else if (is_file($filepath)) {
                // print textfile:
                $t->begin ("textfile");
                $t->addVal ("NAME", $file);
                $t->end ("textfile");
            }

            $t->end ("entry");
        }

        $dir = closedir ($dir);
    }

    // close block, ready for next iteration:
    $t->end ("dirlist");
}


/****************************
* Main script starts here
*****************************/

// include precompiled template:
$t = new phptempt ("list_dir.inc");

// check URL parameter:
if (!isset ($rootfolder))
{
    print "Please provide parameter 'rootfolder'";
    exit;
}

// add to template variable:
$t->addVal ("FOLDERNAME", $rootfolder);

// recursively print contents of project root directory:
listfolder ($rootfolder);

// output template:
$t->out();

?>