breadth first search for files in hirearchy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

breadth first search for files in hirearchy

Post by crystal ship »

I want to search the files in the folders in any depth by breadth first search and have written the following code but the code dose not seems like searching breadth first but it still is searching the files.

Code: Select all

<?
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);

define ('DIRECTORY','../../../sudeep');
include ('queue.php');

function using($pathTree=NULL){
	$objQueue = new Queue;
	$path=ereg_replace("\.","/",$pathTree).".php";	
	$pathsearched=search($path,DIRECTORY,$objQueue);
	echo $pathsearched;
}

function search($path, $directory, &$objQueue){
	$objQueue->Put($directory);
	$targetFragmented = explode("/", $path);
	for ($i=0;$i<count($targetFragmented);$i++){
		$target=$targetFragmented[$i];	
		while ($objQueue->IsEmpty() == false){
			$directory=$objQueue->Get();
			if(is_dir($directory)){
				$direc = opendir($directory);
				while(false !== ($file = readdir($direc))){
					if($file !="." && $file != ".."){
						if(is_file($directory."/".$file)){
							if(strcmp($target, $file)==0){
								//return $directory."/".$file;
								return require_once $directory."/".$file;
								break;
							}							
						}else if(is_dir($directory."/".$file)){
							//echo $target."/".$file;
							//echo "<br>";
							if(strcmp($target, $file)==0){
								$path=explode("/", $path);
								array_shift($path);
								$path=implode("/", $path);
								return search($path,$directory."/".$file,$objQueue);
								break;					
							}
							search($path,$directory."/".$file,$objQueue);
						}						
					}
				}
				closedir($direc);
			}
			
		}
	}
	$objQueue->Clear();
	return;
}
?>
Could any one help me in this code? Thanks in advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What is "breadth first" supposed to mean?
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

Here i assume that the function searches for the file or folder in the first level inside the root directory I have given then if it finds the file then returns the file and if it finds the folder I search, then it searches file inside the found folder in another level (in its descendants). and if it dose not find the any file and folder i want then it returns in the first level. Once it finishes search in the first level then it searches in the lower level descendants in the folders in the first level for the file and folder i want.

Hope this clarify you what I want. If any confusion then please ask.
Here I did not want to search any of the folders twice.

Thank You
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sounds like you want something recursive.
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

Yep everah you are to my point. I need something recursive but isn't the code recursive cause it calls the search function recursively.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are you using PHP 5? If so you might want to look at the RecursiveDirectoryIterator SPL class/interface.
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

Sorry to say but I am using PHP4. Any similar functions that we can find for PHP4 too.
Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

glob() should be of interest.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

Thank You all, I will go through these topics and let you all know if any problem still arises.

Thank You all once again
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Post by devendra-m »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


If you want to search a file in any folder then the following code will be very helpful


test.php

Code: Select all

require(getPath('test/test1.php'));

included.php

Code: Select all

define('ROOT_PATH',$_SERVER['DOCUMENT_ROOT']);
	function traverse($root,$destination)
	{
		$handle = opendir($root);
		while (false !==($file=readdir($handle))) 
			if($file != '.' && $file != '..')
			{
				$full_path=$root."/".$destination;
				if(file_exists($full_path))				
					return $full_path;					
		
				$path=$root."/".$file;
				iif($full_path=traverse($path,$destination))
					return $full_path;
			}
	}
	function getPath($destination)
	{
		return traverse(ROOT_PATH,$destination);
	}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by devendra-m on Tue Jan 01, 2008 11:11 pm, edited 5 times in total.
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

Aye, That's what I was searching for. Thanks :lol:
Post Reply