TOA Posted April 4, 2012 Share Posted April 4, 2012 I found this topic, and followed salathe's posted code (since this is exactly what I need to do). When I just copy/paste his code, it works fine, but when I try to incorporate this into a class, I get the error: "RecursiveDirectoryIterator::__construct(test(a).php) [function.RecursiveDirectoryIterator---construct]: failed to open dir: Not a directory" Now obviously I get what the message means, but I don't understand why I'm getting it, and more specifically why I'm getting it only in the class; as far as I can tell, I simulated the function correctly. Here's my latest code: class DirectoryTraverser { private $iterator; public function traverseRecursive($path) { $this->iterator = new RecursiveDirectoryIterator($path); $list = array(); foreach($this->iterator as $file) { $current = array( 'label' => $file->getFilename(), ); if ($file->isDir()) { $current['children'] = $this->traverseRecursive($this->iterator->getChildren()); } $list[] = $current; } if (empty($list)) return false; return $list; } } Called using: try { $mgr = new DirectoryTraverser; if (($files = $mgr->traverseRecursive('../')) === false) { echo "<p>Error running recursive Traverser</p>"; } else { echo "<p>Recursive Traversal:<br /><pre>"; print_r($files); echo "</pre></p>"; } } catch (Exception $e) { echo $e->getMessage(); } Thanks for reading guys (and gals)! Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/ Share on other sites More sharing options...
TOA Posted April 4, 2012 Author Share Posted April 4, 2012 Oh, I think I see it. I'm passing in an iterator (with iterator->getChildren()) even though it's expecting a path($path). So I tried passing in the path (file->getFilename()) and I get the same error with a different file name. Not quite sure what the dealy-o is... Think I need to re-think this for a sec Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334364 Share on other sites More sharing options...
batwimp Posted April 4, 2012 Share Posted April 4, 2012 I coped the code exactly as you have it and it worked fine on my machine. Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334371 Share on other sites More sharing options...
TOA Posted April 4, 2012 Author Share Posted April 4, 2012 I coped the code exactly as you have it and it worked fine on my machine. It did?!? Thanks for that! Gives me a place to start at least..which is more than I had 5 minutes ago lol Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334373 Share on other sites More sharing options...
batwimp Posted April 4, 2012 Share Posted April 4, 2012 It seems like the path you passed in isn't being seen as an actual directory, which is weird, because it's just an "up one directory" path. Is there a chance that your code is starting in the root directory? Or that the directory above the script has too many read/write restrictions on it? Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334374 Share on other sites More sharing options...
TOA Posted April 4, 2012 Author Share Posted April 4, 2012 It seems like the path you passed in isn't being seen as an actual directory, which is weird, because it's just an "up one directory" path. Is there a chance that your code is starting in the root directory? It shouldn't be because when I run the non-recursive method I get the correct output (although that method is not using the recursive iterator, just the normal one) and it works fine with the 'up one directory' path. It's a possibility though, I'm going to look into that. Or that the directory above the script has too many read/write restrictions on it? The folder shows all the normal permissions. I just tried changing a few of them though and it changes nothing so I don't think this is the issue. Fyi to anyone reading: all the 'files' in that directory are folders (no single files)..might it have something to do with that? Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334377 Share on other sites More sharing options...
batwimp Posted April 4, 2012 Share Posted April 4, 2012 It shouldn't. But you can always throw a file or two in there just to be sure. Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334379 Share on other sites More sharing options...
TOA Posted April 4, 2012 Author Share Posted April 4, 2012 It shouldn't. Didn't think so. But you can always throw a file or two in there just to be sure. Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334381 Share on other sites More sharing options...
TOA Posted April 4, 2012 Author Share Posted April 4, 2012 But you can always throw a file or two in there just to be sure. Nope, it doesn't change anything. Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334382 Share on other sites More sharing options...
batwimp Posted April 4, 2012 Share Posted April 4, 2012 It could be trying to open a directory that isn't an actual directory. Try calling it with a different path, where there are only a couple of files and directories and see what it does. Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334384 Share on other sites More sharing options...
salathe Posted April 4, 2012 Share Posted April 4, 2012 I'm passing in an iterator (with iterator->getChildren()) even though it's expecting a path($path). Bingo. Your $this->iterator->getChildren() would get an iterator for the contents of the directory, then on the first line of traverseRecursive() that iterator (as $path) would be interpreted as a string in the RecursiveDirectoryIterator constructor: when you convert a RecursiveDirectoryIterator into a string, you get the filename of the current iterator item (in your case the first file/directory). So I tried passing in the path (file->getFilename()) and I get the same error with a different file name. That's likely because you only gave it the filename and it was not an immediate subdirectory of the current working directory: you likely want to send the full path from getPathname(). As for where to go from here, well I hope the above explains where you were going a bit wrong. Personally, I'd make your traverseRecursive() method accept a RecursiveDirectoryIterator as the $path (optionally accepting a string too, to make the first call easier) and not bother with manually creating a new one on that first line, each time you call the function. Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334400 Share on other sites More sharing options...
TOA Posted April 4, 2012 Author Share Posted April 4, 2012 you likely want to send the full path from getPathname(). Voila! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334414 Share on other sites More sharing options...
TOA Posted April 4, 2012 Author Share Posted April 4, 2012 Just to clarify something.. Does this mean that this statement was accurate (or at least on the right track), and I just needed to find the correct path? It seems like the path you passed in isn't being seen as an actual directory, which is weird, because it's just an "up one directory" path. Is there a chance that your code is starting in the root directory? Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334415 Share on other sites More sharing options...
TOA Posted April 4, 2012 Author Share Posted April 4, 2012 Personally, I'd make your traverseRecursive() method accept a RecursiveDirectoryIterator as the $path (optionally accepting a string too, to make the first call easier) and not bother with manually creating a new one on that first line, each time you call the function. Just re-reading your post and noticed this. This is obviously good advice. I'm going to try to take it. Not quite sure how yet, but I'll think on it. Thanks for the help and advice. Quote Link to comment https://forums.phpfreaks.com/topic/260346-help-with-recursivedirectoryiterator-desired/#findComment-1334418 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.