0

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?

3
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by diafol
0

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

0

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);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.