Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:27:29+00:00 2026-06-12T23:27:29+00:00

I have the following function as part of a larger program that crawls the

  • 0

I have the following function as part of a larger program that crawls the contents of a provided path, and indexes any .htm or .html pages that it finds in the parent folder or any subfolders. My crawler function (below) is recursive, and seems to work just fine, UNTIL it reaches enters a subfolder that contains no items.

This seems to be a common problem that is often fixed by structuring the while loop as follows:

while ( false !== ($file = readdir($folder)) )

but this isn’t working. The last line that gets output is ‘The current crawler path is …’, and then the output just stops. I’m guessing the problem is the empty folder and the readdir function, but I don’t know how to fix it. Can someone offer a suggestion?

Thanks

function crawlFolders($path)
{
    $prevPath = $path;  // variable to keep track of the previous file path
    chdir($path);
    $folder = opendir($path);

    echo "The current crawler path is ".$path."<br>";

    while ( false !== ($file = readdir($folder)) ) // read current directory item, then advance pointer
    {   
        if ( is_file($file) )
        {   echo "File found!  The crawler is inspecting to see if it can be indexed<br>";
            if ( canIndex($path."/".$file) )
                indexPage($path."/".$file);
        }

        else if ( is_dir($file) ) 
        {
            //it's a folder, we must crawl
            if ( ($file != ".") && ($file != "..") )    //it's a folder, we must crawl
            {
                echo "$file is a folder<br><br>";
                crawlFolders($path."/".$file);
                chdir($prevPath); // change the working dir back to that of the calling fn

            }
        }   
    }
    closedir($folder);

}

After looking at this some more, I can’t see why readdir is causing the problem. I think the problem may be that my crawlFolders function is not unwinding itself, and is instead just ending when it reaches the deepest, empty folder. Am I missing something with the way the recursion should work? I was under the impression that the recursive function calls would exit once the while loop returned false, thus dropping me to the previous crawlFolders function that made the recursive call (i.e. unwinding itself).

Do I need to return a value each time crawlFolders exits, so that the calling function knows where to resume itself?

It definitely seems like the recursion is the problem. I placed a file in the empty folder and my indexer worked, but the functions still didn’t unwind as I wanted. I know this isn’t happening because there are still two files in the starting path that weren’t evaluated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:27:30+00:00Added an answer on June 12, 2026 at 11:27 pm

    The problem isn’t the recursion but very likely the current working directory.

    You change the current directory using chdir() and then with $file you give a relative filename to is_file() and is_dir(). After the execution returns from the recursion the current directory is still the subdirectory so is_file($file) and is_dir($file) won’t find the files.

    You have to save the current directory before you go into the recursion or – better – avoid chdir() altogether and work with full paths: is_file($path . '/' . $file)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following in a program (part of a much larger function, but
I have the following part of a function that is driving me crazy. As
I have the following Javascript which is part of a larger function. I am
I have the following function in JavaScript every part of the if statement executes
I have the following function part of a UIImage Category: - (UIImage *)copyImageAtRect:(CGRect)rect {
I have the following function int namecomp(char c); Part of the function code else
I have the following code as part of a larger web application to search
I have the following vba code is part of a larger script. The issue
To cut a long story short I have the following function as part of
I have the following function which is part of the Trie structure implementation: int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.