recursive class method

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

scottayy wrote:Are you saying that it shouldn't follow . and .. if my coding tells it to? Because before I added the check to skip those directories, it definitely followed them and not only sent me into a very long loop
You will find . and .. in every directory
. references the current directory while .. points to the parent directory.
Therefore if you let the function call itself with '.' as parameter you will get an infinite loop

Code: Select all

while (false !== ($file = readdir($handle)))
{
  $path = $dir.'/'.$file;
  if (is_dir($path))
  {
    if ('.'!=$file && '..'!=$file) {
      $this->findJpgs($path);
    }
  }
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Yes, I realized that was my problem last night. I've done a directory reader lots of times and for some silly reason I forgot that last night. The script began "working" after I excluded those from being evaluated.

However it didn't really "work" because of is_dir() returning false (when in fact it is true). So now I have to halt development for a bit until I feel like developing remotely. :P

I could use the absolute path (which the user comments for is_dir reports is the solution to the bug), but then I'd have to count the levels and stack the directories to give a recursive absolute path (probably should do this anyways but I'm being lazy at the moment).

Thanks for your insight!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply