NYCPHP Meetup

NYPHP.org

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

Anthony Wlodarski aw at sap8.com
Thu Aug 16 10:41:10 EDT 2007


Disregard, 

I thought you meant file browser, not a search script to parse through the
directories.  But maybe this will apply in the while look to discern
directories from files.

Sorry for any confusion.

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


-----Original Message-----
From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org] On
Behalf Of Anthony Wlodarski
Sent: Thursday, August 16, 2007 10:39 AM
To: 'NYPHP Talk'
Subject: RE: [nycphp-talk] Search of directories and files on my web server

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()
{

	if(isset($_GET['currwd']))
	{
		$currwd = $_GET['currwd'];
	}
	else
	{
		// 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
directories.
		if(is_dir($currwd."/".$fh)) //$fh alone will not suffice we
must concatenate with the full path
		{
			echo "<td class='file'><a
href='./filebrowser.php?currwd=".$currwd."/".$fh."'
target='_blank'>".$fh."</a></td>\n";
		}
		else
		{
			echo "<td class='file'><a href='".$currwd."/".$fh."'
target='_blank'>".$fh."</a></td>\n";
		}

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

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

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

	// close the directory handle
	$dh->close();
}// end of funtion printDirectory

// main part of our application
printDirectory();

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

-----Original Message-----
From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org] 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

Greetings,

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  
list.

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):

<?php
$path='/home/usr/account';
search($path);
	
function search($path)
{
	$dir=opendir($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)) {
			$file=file($item);
			$lines=count($file);
			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 />';
				}
			}
		}
	}
	closedir($dir);
}
?>

Cheers,

-Aaron
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com

Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php


_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com

Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php





More information about the talk mailing list