[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 473
  • Last Modified:

PHP - List folders and sub-folders and all files

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):
/folder1/
/folder1/file1.php
/folder1/file2.htm
/folder2/
/folder2/folder22/
/folder2/folder22/file3.php
/folder2/file4.css

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
error_reporting(E_ALL);
echo "<pre>";

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

// READ THE CURRENT WORKING DIRECTORY
if (is_dir($dir))
{
    if ($dh = opendir($dir))
    {
        while (($file = readdir($dh)) !== false)
        {
            echo "filename: $file : filetype: " . filetype($dir . DIRECTORY_SEPARATOR . $file) . PHP_EOL;
        }
        closedir($dh);
    }
}

Open in new window

0
Pedro Chagas
Asked:
Pedro Chagas
1 Solution
 
darren-w-Commented:
<?php
	//require_once("dir.class.php");
	$obj = new read_full_dir('/home/darren/Downloads');
        echo "<pre>";
	print_r($obj->getTree()); 
         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
      $dir_tree
     ================================================*/
  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 );
	    }
	    else
	      $this->dir_tree['files'][] = $directory.'/'.$file;
	  }
	}
    }
  }
  
  function getTree(){
      return $this->dir_tree;
  }
 
}
?>

Open in new window


taken from here:

http://www.php-help.ro/php-tutorials/php-read-directory/

0
 
Pedro ChagasWebmasterAuthor Commented:
Excellent example.
In the future I try to consult more times the php.net site.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now