mpsn Posted November 3, 2011 Share Posted November 3, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/250400-how-to-traverse-complex-directory/ Share on other sites More sharing options...
Wiro Blangkon Posted November 3, 2011 Share Posted November 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250400-how-to-traverse-complex-directory/#findComment-1284796 Share on other sites More sharing options...
mpsn Posted November 4, 2011 Author Share Posted November 4, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/250400-how-to-traverse-complex-directory/#findComment-1284832 Share on other sites More sharing options...
Wiro Blangkon Posted November 5, 2011 Share Posted November 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250400-how-to-traverse-complex-directory/#findComment-1285116 Share on other sites More sharing options...
mpsn Posted November 5, 2011 Author Share Posted November 5, 2011 Yes, I understand that, that's actually a typo, I meant searchFolders! Any help much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/250400-how-to-traverse-complex-directory/#findComment-1285117 Share on other sites More sharing options...
Wiro Blangkon Posted November 5, 2011 Share Posted November 5, 2011 The code says that if $dir is a file (not a folder), all the XML files contained in it should be stored in the DB. That of course is not possible. Wiro. Quote Link to comment https://forums.phpfreaks.com/topic/250400-how-to-traverse-complex-directory/#findComment-1285220 Share on other sites More sharing options...
mpsn Posted November 6, 2011 Author Share Posted November 6, 2011 Thanks for that. I will re-code and go from there! Quote Link to comment https://forums.phpfreaks.com/topic/250400-how-to-traverse-complex-directory/#findComment-1285405 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.