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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:10:58+00:00 2026-06-14T12:10:58+00:00

I had to list all files and folders in a directory: $images = array();

  • 0

I had to list all files and folders in a directory:

            $images = array();
            $dirs   = array();

            $dir = new DirectoryIterator($upload_dir_real);

             foreach ($dir as $file) {
               if ($file->isDot()) {
                    continue;
                }

                if ($file->isDir()) {
                    // dir
                    $scanned_dirs[] = $file->getPath();
                      continue;
                } else {
                    // file

                    //echo $file->getFilename() . "<br>\n";//DEBUG
                    $realfile =  $file->getFilename() . "<br>\n";
                    $realpath = $file->getPathname();
                    echo realpath($realfile);//DEBUG
                    $file->getFilename();
                    $images[] = realpath( $realpath );
                }

             }

This works fine (no errors) but of course counted only the root, so I tried recursive:

            $images = array();
            $dirs   = array();
$dir = new RecursiveDirectoryIterator($upload_dir_real);

             foreach ($dir as $file) {
               if ($file->isDot()) {
                    continue;
                }

                if ($file->isDir()) {
                    // dir
                    $scanned_dirs[] = $file->getsubPath();
                      continue;
                } else {
                    // file

                    //echo $file->getFilename() . "<br>\n"; //DEBUG
                    $realfile =  $file->getsubFilename() . "<br>\n";
                    $realpath = $file->getsubPathname();
                    echo realpath($realfile);//DEBUG
                    $file->getFilename();
                    $images[] = realpath( $realpath );
                }

             }

Basically, I changed the getPath(); with getsubPath() (and equivalent). The problem is that it give me an error:

Fatal error: Call to undefined method SplFileInfo::isDot() in blah blah path

so I searched a while and found this:

Why does isDot() fail on me? (PHP)

This is basically the same problem, but when I try, I get this error:

Fatal error: Class 'FilesystemIterator' not found in in blah blah path

Questions:

1 – why is the method described in the other accepted answer not working for me?
2 – in that same answer, what is the following code:

new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator(
    $pathToFolder,
    FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_SELF));

This actually calls RecursiveIteratorIterator twice? I mean, if it is recursive, it can not be recursive twice 🙂

2b – how come FilesystemIterator is not found, even if the PHP manual states (to my understanding) that it is a part of what the recursive iterator is built upon?

(Those questions are because I want to understand better, not to just copy and paste answers).

3 – is there a better way to list all folders and files cross platform?

  • 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-14T12:10:59+00:00Added an answer on June 14, 2026 at 12:10 pm
    • 1 – why is the method described in the other accepted answer not working for me ??`

    As far as i can tell . the code works perfectly but your implementation is wrong you are using the following

    Code

       $dir = new RecursiveDirectoryIterator($upload_dir_real);
    

    Instead of

        $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($upload_dir_real));
    
    • In that same answer actually calls RecursiveIteratorIterator twice ?? I mean, if it is recursive , it can not be recursive twice … :-))

    No it does not its different

    RecursiveIteratorIterator != RecursiveDirectoryIterator != FilesystemIterator
                ^                             ^                    
    
    • how come FilesystemIterator is not found , even if the php manual states (to my understanding) that it is a part of what the recursive iterator is built upon??

    You already answered that your self in your comment you are using PHP version 5.2.9 which is no longer supported or recommended

    • 3 – Is there a better way to list all folder and files cross platform ??

    Since that is resolved all you need is FilesystemIterator::SKIP_DOTS you don’t have to call $file->isDot()

    Example

    $fullPath = __DIR__;
    $dirs = $files = array();
    
    $directory = new RecursiveDirectoryIterator($fullPath, FilesystemIterator::SKIP_DOTS);
    foreach (new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST) as $path ) {
        $path->isDir() ? $dirs[] = $path->__toString() : $files[] = realpath($path->__toString());
    }
    
    var_dump($files, $dirs);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a list of all the folders in the c:\Users\UserName
I want to loging all programs and files what user had opened, to create
I'm using a PHP webpage to give me a list of all files with
Is there any way to list all the files that have changed between two
In my solution a had a folder with a few files. All this files
I have this code: filename = tkFileDialog.askopenfilename(initialdir=lists/custom/, filetypes=((Word list, *.tldr), (All files, *.*))) If
If i had a list of balls each of which has a color property.
I heard this today during interview for java developer. I had to list some
I Had Drop down list and I want to fill it with data from
Let's say I had: protected void performLogic(List<Object> docs) { ... } In the code

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.