#native_company# #native_desc#
#native_cta#

Complete recursive directory listing

By Dominique Stender
on July 28, 2001

Version: 0.2

Type: Function

Category: File Management

License: GNU General Public License

Description: This snippet will take a given path and list every file/directory under that path. Uses recursion and lists files/dirs in depth level.

<?php
/*
   Complete Directory Listing (recursive)
   by Dominique Stender ([email protected])
   based on a script by James Holden ([email protected])
   
   Version 0.2
   
   

   array RecurseDir( string directory );
   
   
   
   
-- Description -----------------------------------------------------------------
   
     This script recurses a directory on the server and puts its content
     in an array. Each subdirectory is put into a subarray.
     The array for each single directory has the following structure:
   
     $thisdir = array("name"   => "absolute path of the direcory",
                      "struct" => array());
                    
     As you can see the path itself is put into the "name" field of the
     array while the "struct" field is an array.
     For each file the function finds it adds an entry to the next 
     "struct" field containing the files' name.
     For each directory the function finds it adds an entry to the next
     "struct" field containing a new array of the described structure.
   
     There are two exceptions:
     unaccessible directories have a scalar struct = -1
     empty directories have a scalar struct = -2
     
     After the function has recursed the whole directory it returns the array
     
-- Example ---------------------------------------------------------------------
     
     When I recurse a part of my apache htdocs directorytree which contains
     some images the array looks like this (PHP comments not included).
     Each subarray and therefore each subdirectory is indicated by leading
     whitespace:
     
name = /usr/local/apache/htdocs/media                  // this is where we start
struct
  name = /usr/local/apache/htdocs/media/.1             // first entry is a dir
  struct
    name = /usr/local/apache/htdocs/media/.1/.3        // same here, dir again
    struct                                             // this dir has content
      0 = 40.jpg                                       // 0 is the arrays key
      1 = tn_40.jpg                                    // the *.jpg's are the value
      2 = 41.jpg
      3 = tn_41.jpg
      4 = 42.jpg
      5 = tn_42.jpg
      6 = 43.jpg
      7 = tn_43.jpg
      8 = 44.jpg
      9 = tn_44.jpg
      10 = 45.jpg
      11 = tn_45.jpg
    name = /usr/local/apache/htdocs/media/.1/.4        // next subdirectory
    struct = -1                                        // access denied!
    name = /usr/local/apache/htdocs/media/.1/.5        // next subdirectory
    struct                                             // content again
      0 = 81.jpg
      1 = tn_81.jpg
      2 = 82.jpg
      3 = tn_82.jpg
      4 = 83.jpg
      5 = tn_83.jpg
      6 = 84.jpg
      7 = tn_84.jpg
      8 = 85.jpg
      9 = tn_85.jpg
      10 = 86.jpg
      11 = tn_86.jpg
      12 = 87.jpg
      13 = tn_87.jpg
      14 = 88.jpg
      15 = tn_88.jpg
    name = /usr/local/apache/htdocs/media/.2           // next subdirectory
    struct
      name = /usr/local/apache/htdocs/media/.2/.6      // another subdir
      struct = -2                                      // this one is empty!
   
-- Disclaimer ------------------------------------------------------------------
   
   This script is provided as-is. I take no responsibility on any misbehavior,
   errors, damage or whatever else this script might cause.
   As the original script by James Holden did not clearly say under which
   license it was published I decided to make it freeware. You can use this
   script for any purpose you want, non-commercially and commercially alike,
   and you're allowed to change the code to whatever pleases you.
   You do not need to republish the resulting script as freeware.
   
   If this license offends the intended license by James Holden please let me
   know and I will change it as necessary.
   
*/

	function RecurseDir($directory) {
		$thisdir = array("name", "struct");
		$thisdir['name'] = $directory;
		if ($dir = @opendir($directory)) {
		  $i = 0;
			while ($file = readdir($dir)) {
				if (($file != ".")&&($file != "..")) {
					$tempDir = $directory."/".$file;
					if (is_dir($tempDir)) {
					  $thisdir['struct'][] = RecurseDir($tempDir,$file);
					} else {
					  $thisdir['struct'][] = $file;
					} 
			  	$i++;
				} 
			} 
			if ($i == 0) {
			  // empty directory
			  $thisdir['struct'] = -2;
			}
		} else {
		  // directory could not be accessed
			$thisdir['struct'] = -1;
		}
		return $thisdir;
	}
?>