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:

<?php
session_start();
?>
<html>
<head><title>page</title>
</head>
<body>
<?php
$dir=opendir("E:/DFS/Las_Vegas/intranet");
$files=array();
while (($file=readdir($dir)) !== false)
{
    if ($file != "." and $file != ".." and $file != "index.php")
    {
        array_push($files, $file);
    }
}
closedir($dir);
sort($files);
foreach ($files as $file)
    print "<a href='http://lmtl-files/intranet/files/$file'>$file</a><br />";
?>
</body>
</html>

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 for diafol

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 for diafol

A fiddled example from the php manual:

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

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.