Page 1 of 1

breadth first search for files in hirearchy

Posted: Sun Dec 23, 2007 11:04 pm
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.

Posted: Sun Dec 23, 2007 11:16 pm
by feyd
What is "breadth first" supposed to mean?

Posted: Sun Dec 23, 2007 11:28 pm
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

Posted: Sun Dec 23, 2007 11:31 pm
by RobertGonzalez
Sounds like you want something recursive.

Posted: Sun Dec 23, 2007 11:36 pm
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.

Posted: Sun Dec 23, 2007 11:37 pm
by RobertGonzalez
Are you using PHP 5? If so you might want to look at the RecursiveDirectoryIterator SPL class/interface.

Posted: Sun Dec 23, 2007 11:47 pm
by crystal ship
Sorry to say but I am using PHP4. Any similar functions that we can find for PHP4 too.
Thanks

Posted: Sun Dec 23, 2007 11:52 pm
by feyd
glob() should be of interest.

Posted: Sun Dec 23, 2007 11:53 pm
by John Cartwright

Posted: Mon Dec 24, 2007 12:08 am
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

Posted: Mon Dec 31, 2007 11:24 pm
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]

Posted: Mon Dec 31, 2007 11:27 pm
by crystal ship
Aye, That's what I was searching for. Thanks :lol: