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
https://forums.phpfreaks.com/topic/250400-how-to-traverse-complex-directory/
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.

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!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.