#native_company# #native_desc#
#native_cta#

recursive directory display

By Russell Michell
on October 25, 2001

Version: 1.3

Type: Sample Code (HOWTO)

Category: File Management

License: BSD License

Description: This simple code fragment takes a directory, and recurses it, displaying all the files and directories underneath that directory. A good general purpose framework for a utility that needs to recurse a directory and operate on each file or something like that.

#Function to recurse thru a directory and:
#1: Displays dir-contents in a new table row
#2: Gets the last modified date of each file in this dir
#3: Displays an image: 'new.gif' uf this file has been modified in the last 7days
#4: Gets the title of the page from between the <title></title> tags

#Call the function from any file within the dir to be searched thus:
<?php
include("path/to/include/dircontents.php");
?>

#dircontents.php
<?php 
//Display contents of a directory, start off display table with headings
echo "
<table width="600" border="0">n
<tr>nt
<th align="left">&nbsp;</th>n<th align="left" width="150">Files in this folder:</th>n<th align="left">&nbsp;</th>n<th align="left" width="100">Modified:</th>n<th align="left" width="200">Page Title:</th>n
</tr>n
"; 
//Get contents of current directory using the getcwd() function, and pass it thru the parse_dir() function in: path/to/include/dircontents.php
$scanthis = getcwd();
parse_dir($scanthis);
//Finish off display table
echo "</table>n";
?>

#Place this in a 'functions.php' include file or directly in a file within the dir to searched. (I did the former, it's easier to maintain)
#This function written by a bunch of other people - completely overhauled with new functionality by me: [email protected] 2001 - if you modify it I'd #appreciate an email so that I can see what you did. (breaking up into separate functions would be nice :-)
#If it doesn't violate what others believe then call it freeware but with no guarnatee's from me!! Use at own risk.
#$scanthis = Dir to be analaysed, $rootDir = Dir to take as root

<?php
function parse_dir($scanthis) {
	$rootDir = "http://your.domain.com/";
	$dir = @opendir($scanthis);
	while (false!=($file = @readdir($dir))) {
		if ($file != "." && $file != "..") {
			$scanthispp = preg_replace("/s/","%20",$scanthis);
			$filep = preg_replace("/s/","%20",$file);
			$theFile = $scanthispp . "/" . $filep;
			$scanthispp = substr($scanthis,25);	//trim absolute file path
			$modDate = filemtime($theFile); //get last modified date of file
			$modDate = date("Y-m-d", $modDate);
			$fp = fopen("$theFile",'r'); //get title of page
			$buffer = fgetss($fp,100,"<html lang="en"><head><title>");
			$buffer = ereg_replace("</title>", "", $buffer);
			$title = substr($buffer,20);
			$yr = substr($modDate,0,4); //display 'new' image if item not-older than 7days
			$mnth = substr($modDate,5,2); 
			$dy = substr($modDate,8,2); 
			$newday = date("Y-m-d", mktime(0,0,0,$mnth,$dy + 7)); 
			$now = date("Y-m-d");
			if ($now <= $newday) {
				$img = "<img src="$rootDir/images/new.gif" alt="This item updated in the last week!">";
				}
			//Display alternate row colours
			$color_1 = "#CCCCCC";
			$color_2 = "#33FFFF";
			static $color;
			if ($color == $color_1) {
				$color = $color_2;
				}
			else {
				$color = $color_1;
				}
			$dispThis .= "<tr bgcolor='$color'>n<td align="left" valign="middle"></td>n<td align="left" valign="middle" width="150"><p><a class="fileText" href="$rootDir$scanthispp/$filep">$scanthispp/$filep</a></p></td>n<td align="left" valign="top">$img</td>n<td align="left" valign="middle" width="100"><span class="fileText">$modDate</span></td>n<td align="left" valign="middle" width="200"><p>$title</p></td>n</tr>n";
			}
		}
	echo $dispThis;
	}
?>