Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pedro Chagas

ASKER

Excellent example.
In the future I try to consult more times the php.net site.