Jump to content

how to traverse complex directory


mpsn

Recommended Posts

Hi, currently I have a function searchFolder that simply takes in ONE directory which holds all the xml files I wish to store to db via function writeXMLtoDBviaDOM.

 

I want to make it more robust(realistic) to be able to also search complex directories (so the main directory I pass as parameter to searchFolders) in turn holds sub-directory (which in turn holds further sub-directory) which finally holds all the xml files.

 

here is code:

<?php
  $dir = "C:/dir/dir2/dir3/";
  function searchFolders($dir) {
        $_filePath="";
$filePathsArray=array();//stores each file 
$xmlFiles = glob($dir . '*.xml');

if(is_dir($dir)) { 
           // list files
   $xmlFiles = glob($dir . '*.xml'); 

   //print each file name		
   foreach($xmlFiles as $xmlFile) {
	print $xmlFile."<br />";
	$filePathsArray[]=$xmlFile;
   }
}//END BIG IF retreiving all files in dir param

foreach($filePathsArray AS $curFile) {
	//set up db connection
	mysql_connect("localhost","root");
	mysql_select_db("someDb");

	$_filePath=$curFile;
	$dom=new DOMDocument();
	$node=basename($_filePath);//$node is the file name only
	$dom->load($node);
	writeXMLtoDBViaDOM($dom->documentElement,$_filePath); 

}//END FOR-EACH LOOP for each XML file
    }//END FCN searchFolders VERSION 0

    searchFolders($dir);//VERSION 0 ONLY searched 1 simple dir 		
?>	

 

Any help much appreciated!

Link to comment
Share on other sites

Hello MX,

 

1) You might want to do your msyql_connect only once, before you start your recursive function.

2) Do you have a particular reason for $_filePath = $curFile ?

3) My suggestion for the recursive algorithm in pseudocode:

function storeXMLfiles($root_dir) {
  // TODO: get $root_dir contents and put them in an array called $contents
  foreach ($contents as $content) {
     if (is_dir($root_dir.'/'.$content) {
        storeXMLfiles($root_dir.'/'.$content);
     } else {
        // TODO: Do your XML storing magic here
     }
  }
}

 

Good luck with it.

 

Wiro.

Link to comment
Share on other sites

My recursion is not that good, so now I get an infinite loop for the following:

 

<?php
$dir = "C:/dir1/dir2/dir3/";
function searchFolders($dir) {
//FIRST get root dir and put into an array
$startDir=scandir($dir);
$filePathsArray=array();

foreach($startDir AS $curDir) {
	if(is_dir($dir)) {			
		searchFoldersRecursively($curDir);
	}
	else if(is_file($dir)) {//BASE CASE when no more sub-dir for current dir
		$xmlFiles=glob($dir."*.xml");
		//FIRST STORE EACH file
		foreach($xmlFiles AS $curXMLfile) {
			$filePathsArray[]=$curXMLfile;
                                //NOW STORE EACH XML file of this current dir
	                foreach($filePathsArray AS $_filePath) {
                                      writeXMLtoDBviaDOM($_filePath);
	                }//END FOR-EACH LOOP store XML to DB
	}//END ELSE-IF		
}//END BIG FOR-EACH LOOP	
}//END FCN searchFoldersRecursively */

searchFolders($dir);
?>

 

Any help much appreciated!

Link to comment
Share on other sites

Hey MX,

 

The jizzt of recursion is that a function does a call on itself. In your function, I see function X calling function Y, but it should call function X again, although with different parameters. Think of your complex directories as a tree, and that every call to your function puts you at a junction in the tree.

 

Best regards,

 

Wiro.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.