PHP - List folders and sub-folders and all files

Posted on 2011-10-31
Last Modified: 2012-06-22
Hi E´s,
I need a solution for list folders and sub folders and all files inside that folders and sub-folders.
In snippet code I put a php code that has been write by Ray Pauser, and that code list a specific folder and give me the contain.
But if that folder contain more folders and sub folders, and that folders contain files inside?
I need to list like a tree, and in the end I need the output in this way (can be a array):

the order is not important.
What changes I have to do in the code in snippet code?

The best regards, JC

<?php // RAY_temp_joao.php
echo "<pre>";

// the dir is the same where this script will run
$dir = "/";

if (is_dir($dir))
    if ($dh = opendir($dir))
        while (($file = readdir($dh)) !== false)
            echo "filename: $file : filetype: " . filetype($dir . DIRECTORY_SEPARATOR . $file) . PHP_EOL;

Open in new window

Question by:Pedro Chagas
    LVL 13

    Accepted Solution

    	$obj = new read_full_dir('/home/darren/Downloads');
            echo "<pre>";
             echo "</pre>";
    class read_full_dir
      var $dir_tree;
         class constructor
      function read_full_dir( $path_to_dir )
        $this->read_directory( $path_to_dir );
          reads the full directory tree and store it in
      function read_directory( $directory )
        if ( $handle =     @opendir( $directory ) )
    	while ( false !== ( $file = readdir( $handle ) ) )
      	  if ( $file != ".." && $file != "." )
    	    if( is_dir( $directory.'/'.$file))
    	      $this->dir_tree['directories'][] = $directory.'/'.$file;
    	      $this->read_directory( $directory.'/'.$file );
    	      $this->dir_tree['files'][] = $directory.'/'.$file;
      function getTree(){
          return $this->dir_tree;

    Open in new window

    taken from here:

    LVL 3

    Author Closing Comment

    by:Pedro Chagas
    Excellent example.
    In the future I try to consult more times the site.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now