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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:29:36+00:00 2026-06-14T13:29:36+00:00

during my quest for some basic php knowledge , I tried to do a

  • 0

during my quest for some basic php knowledge , I tried to do a simple function to read files and dirs that will not use recursive iterators. (more of an exercise but actually needed for any php<5.3 servers)

the function works great – but when i tried to add a switch ($output) – it no longer lists all the files (for example, before adding the switch, it can see 61 files in 4 folders (3+root) but with the added switch it can only see 47 (only root) .
am I doing something wrong here ? why does the switch statement break the faux “recursive-nest” ??

    function o99_list_my_files_scandir($dir,$output) 
    { 

        $result = $result2 = array(); 

        $root = scandir($dir); 

        foreach($root as $value) 
        { 

          if($value === '.' || $value === '..' || $value == 'Thumb.db' || $value == 'Thumbs.db' || $value == '') { continue;} 

            if(is_file("$dir/$value")) {$result[]="$dir/$value"; continue;} // array files
            if(is_dir("$dir/$value")) {$result2[]="$dir/$value"; continue;} // array dirs

            foreach(o99_list_my_files_scandir("$dir/$value") as $value) 

            { 
                $result[] = $value; 
            }
        } 
            // this is the troublemaker ...

            switch ($output) {
                case 'dirs' :
                return array_filter($result2);  // clean empty results of dirs
                break;

                case 'files' :
                return array_filter($result);   // Clean empty array of files
                break; 
            }

} 

is there any other simple way to get the function to return separate lists of dirs AND files and not a unified array ?

  • 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-14T13:29:37+00:00Added an answer on June 14, 2026 at 1:29 pm

    Because in this line…

    foreach(o99_list_my_files_scandir("$dir/$value") as $value)
    

    …you don’t give $output to the recursively called function, so it won’t output anything. Change it to:

    foreach(o99_list_my_files_scandir("$dir/$value", $output) as $value)
    

    Actually you should have gotten a warning about the missing argument.

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

Sidebar

Related Questions

During a code review I've come across some code that defines a simple structure
During an interview I was asked if there can be some object that will
During the parsing of certain xml files, I come across a situation that I
During a log inspection of my ASP.NET MVC application I discovered that some users
During the installation of my app, I want to create a PostgreSQL-Database and some
During some of the page transitions (for example during the turn and flow transitions)
During our build process, we run sqlldr to populate our database with some sample
During a recent peer review, another Software Engineer suggested that I state that the
During my build I generate a build.properties files via the maven properties plugin (properties-maven-plugin)
During normal browser caching, will a browser re-fetch all file contents if the web

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.