Hey everyone,

I am trying to write a script that goes through a certain directory and all its sub directories and have it return ONLY a list of FILES (not directories).

I have this working good so far for a single directory:

while (($file=readdir($dir)) !== false)
    if ($file != "." and $file != ".." and $file != "index.php")
        array_push($files, $file);
foreach ($files as $file)
    print "<a href='http://lmtl-files/intranet/files/$file'>$file</a><br />";

But I would like it to be able to scan all sub directories inside that directory and ONLY list files.

I would like to have 3 different sub directories:

intranet(parent directory)
IT (sub directory 1)
Accounting (sub directory 2)
Credit (sub directory 3)

I am trying to find a way to have it use the folder name as the title of the <ul> and then list its files right below it.

Anyone know a good way to go about this?

Member Avatar

Had a look at the SPL - 'Standard PHP Library' functions? Got loads of file and directory iterating goodies. Think you need php5+ though.

Member Avatar

A fiddled example from the php manual:

$directory = "myfolder/";
$fileSPLObjects =  new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($directory),
try {
    foreach( $fileSPLObjects as $fullFileName => $fileSPLObject ) {
        echo $fileSPLObject->getFilename() . "<br />";
catch (UnexpectedValueException $e) {
    printf("Dir [%s] contained a dodgy folder", $directory);