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

  • Home
  • SEARCH
  • 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 6142249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:21:42+00:00 2026-05-23T18:21:42+00:00

I have the following function that enumerates files and directories in a given folder.

  • 0

I have the following function that enumerates files and directories in a given folder. It works fine for doing subfolders, but for some reason, it doesn’t want to work on a parent directory. Any ideas why? I imagine it might be something with PHP’s settings or something, but I don’t know where to begin. If it is, I’m out of luck since this is will be running on a cheap shared hosting setup.

Here’s how you use the function. The first parameter is the path to enumerate, and the second parameter is a list of filters to be ignored. I’ve tried passing the full path as listed below. I’ve tried passing just .., ./.. and realpath('..'). Nothing seems to work. I know the function isn’t silently failing somehow. If I manually add a directory to the dirs array, I get a value returned.

$projFolder = '/hsphere/local/home/customerid/sitename/foldertoindex';
$items = enumerateDirs($projFolder, array(0 => "Admin", 1 => "inc"));

Here’s the function itself

function enumerateDirs($directory, $filterList)
{
    $handle = opendir($directory);
    while (false !== ($item = readdir($handle)))
    {
        if ($item != "." && $item != ".." && $item != "inc" && array_search($item, $filterList) === false)
        {
            $path = "{$directory->path}/{$item}";

            if (is_dir($item))
            {
                $tmp['name'] = $item;
                $dirs[$item] = $tmp;
                unset($tmp);
            }
            elseif (is_file($item))
            {
                $tmp['name'] = $item;
                $files[] = $tmp;
                unset($tmp);
            }
        }
    }

    ksort($dirs, SORT_STRING);
    sort($dirs);

    ksort($files, SORT_STRING);
    sort($files);

    return array("dirs" => $dirs, "files" => $files);
}
  • 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-05-23T18:21:43+00:00Added an answer on May 23, 2026 at 6:21 pm

    You are mixing up opendir and dir. You also need to pass the full path (including the directory component) to is_dir and is_file. (I assume that’s what you meant to do with $path.) Otherwise, the functions will look for the corresponding file system objects in the script file’s directory.

    Try this for a quick fix:

    <?php
    
    function enumerateDirs($directory, $filterList)
    {
        $handle = dir($directory);
    
        while (false !== ($item = $handle->read()))
        {
            if ($item != "." && $item != ".." && $item != "inc" && array_search($item, $filterList) === false)
            {
                $path = "{$handle->path}/{$item}";
    
                $tmp['name'] = $item;
    
                if (is_dir($path))
                {
                    $dirs[] = $tmp;
                }
                elseif (is_file($path))
                {
                    $files[] = $tmp;
                }
    
                unset($tmp);
            }
        }
    
        $handle->close();
    
        /* Anonymous functions will need PHP 5.3+. If your version is older, take a
         * look at create_function
         */
        $sortFunc = function ($a, $b) { return strcmp($a['name'], $b['name']); };
    
        usort($dirs, $sortFunc);    
        usort($files, $sortFunc);
    
        return array("dirs" => $dirs, "files" => $files);
    }
    
    $ret = enumerateDirs('../', array());
    
    var_dump($ret);
    

    Note: $files or $dirs might be not set after the while loop. (There might be no files or directories.) In that case, usort will throw an error. You should check for that in some way.

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

Sidebar

Related Questions

I have the following function that works in Delphi 2006, but under Delphi XE2
I have the following code that works on Firefox and Chrome $("#adicionarItem").click(function(){ $.ajax({ type:
I have the following function that draws some data from a chi-squared distribution and
I have the following function that I am so close to cracking but can't
I have the following function that works properly, except for when you use the
I have the following function that is pulling data from a database. The ajax
I have the following function that deletes the LaTeX command surrounding the current cursor
I have the following function that I am using to remove the characters \04
I have the following function that takes a number like 5 and creates a
I have the following situation: I have a certain function that runs a loop

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.