[nycphp-talk] Search of directories and files on my web server

Anthony Wlodarski aw at
Thu Aug 16 10:38:37 EDT 2007

I wrote this file browser of mine a long time ago, non recursive version.
Take a look at the code and hopefully it will help you think of parsing
directories in a different way (but if you had to do it recursively think of
it as traversing a tree structure).  Place this in the root directory and
that is all.

// This php program is a file browser with an upload utility built at the
top just in case you don't have ftp access.
// Anthony W. 1/30/07
// Use at own risk

function printDirectory()

		$currwd = $_GET['currwd'];
		// we are starting from the top most directory
		$currwd = ".";

	//fieldset for our current working directory
	echo "<fieldset>\n";
	echo "<legend>Current working directory</legend>\n";
	echo "<p id='cwd'>".$currwd."</p>\n";
	echo "</fieldset>\n";

	//fieldset for our directory listing
	echo "<fieldset>\n";
	echo "<legend>Directory Listing</legend>\n";
	echo "<table>\n";

	// now we output the directory information including how to
recursively use this script if need be
	// create a directory handle
	$dh = dir($currwd);

	// how to properly traverse a directory
	while(false !== ($fh = $dh->read()))
		echo "<tr>\n";
		// now if it is a file or directory we view file and travers
		if(is_dir($currwd."/".$fh)) //$fh alone will not suffice we
must concatenate with the full path
			echo "<td class='file'><a
			echo "<td class='file'><a href='".$currwd."/".$fh."'

		echo "</tr>\n";
	}// end of while

	// close our table
	echo "</table>\n";

	// close our fieldset
	echo "</fieldset>";

	// close the directory handle
}// end of funtion printDirectory

// main part of our application

Anthony Wlodarski
Senior Technical Recruiter
Shulman Fleming & Partners
646-285-0500 x230
aw at

-----Original Message-----
From: talk-bounces at [mailto:talk-bounces at] On
Behalf Of Aaron Fischer
Sent: Thursday, August 16, 2007 10:17 AM
To: NYPHP-Talk
Subject: [nycphp-talk] Search of directories and files on my web server


I am working on a little script that will start at a specified  
directory on my web server and then proceed to look through all files  
and sub-folders for an instance of text located in a file.  (This is  
a follow-up of sorts to a previous post of mine from a week or two ago.)

I got the code to work fine for searching through one directory.   
However, my thought was that in order to drill down through an  
undefined number of sub-folders I would need to implement a recursive  
function.  The recursive "depth first" search function I made is not  
working as expected and so far I haven't been able to figure out what  
or how I need to tweak it.

I'm completely squeezed for time at work so I'll continue to bang  
away at it but I thought I would post it here in the hopes that some  
of our rockets scientists can show me the error of my ways.  I'm  
guessing this will be a rather simple problem for some folks on the  

So here goes.  Listed below are my algorithm and code.  I'm looking  
forward to finding out where I'm going wrong.  =)

My algorithm:
1) Set the directory that the script will start in.
2) Open that directory and read all files and folders in that directory.
3) While each item is being read, check it.  If the item is a  
directory, call the recursive search in order to drill down further.   
If the item is a file, open the file and search for the text string  
that I am trying to locate.

My code (with comments):

function search($path)
	// browse all files and folders in the current directory
	while (false !== ($item=readdir($dir))) {
		// if the item is a directory, drill down using a recursive
call to  
the search function
		if (is_dir($item) && $item !='.' && $item!='..') {
			search($path . '/' . $item);
		if (is_file($item)) {
			for ($i=0; $i<$lines; $i++) {
				if (strstr(($file[$i]), 'text to search
for')) {
					echo 'Search string found on line '
. $i . ' in file ' . $path .  
'/' . $item . '<br />';


New York PHP Community Talk Mailing List

NYPHPCon 2006 Presentations Online

Show Your Participation in New York PHP

More information about the talk mailing list