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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:04:57+00:00 2026-05-16T12:04:57+00:00

I’m attempting to use a sample function on the PHP manual website (posted by

  • 0

I’m attempting to use a sample function on the PHP manual website (posted by a user). The function is supposed to return a nested array of all of the files and/or folders in a specified directory of your server.

I’m just learning php, so I think it’s more likely that I’m doing something wrong, and less likely that the function doesn’t work. But if there is an easier method than the function below please let me know.

I’m able to iterate through and echo all of the files in the base path, but not the files in my sample sub-folder. The index where the sub-folder should be is either blank or just the folder name and not an array depending on the parameters I use. When I was able to get the folder name, I tested it with is_array which returned false.

Here is the function:

list_array: return an array containing optionally all files, only directories or only files at a file system path

  • @param $base_path string either absolute or relative path

  • @param $filter_dir boolean Filter directories from result (ignored except in last directory if $recursive is true)

  • @param $filter_files boolean Filter files from result

  • @param $exclude string Pipe delimited string of files to always ignore

  • @param $recursive boolean Descend directory to the bottom?

  • @return $result_list array Nested array or false

    function directory_list($directory_base_path, $filter_dir = false, $filter_files = false, $exclude = ".|..|.DS_Store|.svn", $recursive = true){
    $directory_base_path = rtrim($directory_base_path, "/") . "/";
    
    if (!is_dir($directory_base_path)){
        error_log(__FUNCTION__ . "File at: $directory_base_path is not a directory.");
        return false;
    }
    
    $result_list = array();
    $exclude_array = explode("|", $exclude);
    
    if (!$folder_handle = opendir($directory_base_path)) {
        error_log(__FUNCTION__ . "Could not open directory at: $directory_base_path");
        return false;
    }else{
        while(false !== ($filename = readdir($folder_handle))) {
            if(!in_array($filename, $exclude_array)) {
                if(is_dir($directory_base_path . $filename . "/")) {
                    if($recursive && strcmp($filename, ".")!=0 && strcmp($filename, "..")!=0 ){ // prevent infinite recursion
                        error_log($directory_base_path . $filename . "/");
                        $result_list[$filename] = directory_list("$directory_base_path$filename/", $filter_dir, $filter_files, $exclude, $recursive);
                    }elseif(!$filter_dir){
                        $result_list[] = $filename;
                    }
                }elseif(!$filter_files){
                    $result_list[] = $filename;
                }
            }
        }
        closedir($folder_handle);
        return $result_list;
    }
    

    }

Here is one of the many things I’ve tried. The array contains 5 elements, but only 4 of them display. Echoing $array[4] is blank. I’m just trying to get the nested array at this point. But if there is an easier way to iterate through it, any tips are much appreciated.

$directory_base_path = "../sandbox/";
$filter_dir = false;
$filter_files = false;
$exclude_files = ".|..|.DS_Store|dir_read_test.php";
$recursive = true;
$array = directory_list($directory_base_path, $filter_dir, $filter_files, $exclude_files, $recursive);
$array_length = count($array);
echo $array_length;
echo "<br/>";
$count = 0;

while ( $array_length >= $count ) {
 echo $array[$count];
 echo "<br/>";
 $count = $count + 1;
}
  • 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-16T12:04:57+00:00Added an answer on May 16, 2026 at 12:04 pm

    Have a close look at that routine:

        $result_list[$filename] = directory_list(/*recursive*/);
     else
        $result_list[] = $filename;
    

    What’s the difference? The recursive entries are put under index $filename, while the direct contents are put under an ascending integer index, using []. Your loop will only show the integer indexes.

    If you would

    print_r($array);
    

    you will see all entries. (Or you could replace [$filename] with [], of course.) To loop through them yourself, replace the while loop with

    foreach ( $array as $k=>$v )
    {
        echo "$k: $v<br/>";
    }
    

    (for the subdir, this will show “subdir: Array”, but that’s another topic; you could test is_array($v) and take some action based on that)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.