Jump to content

Help with RecursiveDirectoryIterator desired


TOA

Recommended Posts

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)!

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

I coped the code exactly as you have it and it worked fine on my machine.

 

It did?!?  :wtf:

 

Thanks for that! Gives me a place to start at least..which is more than I had 5 minutes ago lol

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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.