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 8726991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:21:27+00:00 2026-06-13T08:21:27+00:00

I have a function getting the recursive folder’s filename inside a specific path: function

  • 0

I have a function getting the recursive folder’s filename inside a specific path:

function getDirectory( $path = '.', $level = 0 ){

$ignore = array( 'cgi-bin', '.', '..');
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.

$dh = @opendir( $path );
// Open the directory to the handle $dh
$files_matched = array(); 


while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory

    if( !in_array($file, $ignore ) && !preg_match("/^.*\.(rar|txt)$/", $file) ){
    // Check that this file is not to be ignored

        if( is_dir( "$path/$file" ) ){
        // Its a directory, so we need to keep reading down...

            echo "<strong>$spaces $file</strong><br />";

            getDirectory( "$path/$file", ($level+1) );
            // Re-call this same function but on a new directory.
            // this is what makes function recursive.

        } else {
            $files_matched[$i] = $file;
            $i++;
        }

    }

}

closedir( $dh );
// Close the directory handle
return $files_matched;
}

echo "<pre>";
$files = getDirectory("F:\Test");
foreach($files as $file) printf("%s<br />", $file);
echo "</pre>";

I used $files_matched to stored the filename in an array.

And for the above result, it only display the filename under “F:\test”.

Actually, I have a sub-folder under “F:\test”. How can I display those filename using the array for storage?

If I modified the code:

$files_matched[$i] = $file;
$i++;

into:

echo "$files<br />";

This will be worked fine and I just don’t know why use array to store the filename for later process is not work??

Thanks for help.

  • 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-13T08:21:27+00:00Added an answer on June 13, 2026 at 8:21 am

    I don’t remember where I got this code from, but it works.

    <?php
    
    function getDirectoryTree( $outerDir , $x){
        $dirs = array_diff( scandir( $outerDir ), Array( ".", ".." ) );
        $dir_array = Array();
        foreach( $dirs as $d ){
            if( is_dir($outerDir."/".$d)  ){
                $dir_array[ $d ] = getDirectoryTree( $outerDir."/".$d , $x);
            }else{
                if (($x)?ereg($x.'$',$d):1)
                $dir_array[ $d ] = $d;
            }
        }
        return $dir_array;
    }
    
    var_dump( getDirectoryTree(getcwd(),'') );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function (bobB) that seems to be getting stuck in a while
I have this function where I am getting the name of a Venue: var
I have a function which creates an object of values, but im getting this
I have a problem with getting my pager-function to work. For some reason it
I have created a custom function and I am getting this error. I dont
I'm getting Reaching end of non-void function warning, but don't have anything else to
I have mini recursive function which finds and hides all .mta files. It looks
I have a function checkAllInOneDirection which is a recursive loop. When I break out
I have a recursive function which index a textile and if I use it
I'm getting weird behavior out of [NSDate timeIntervalSinceReferenceDate] . I have the following function:

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.