Display Directory Contents with PHP

by admin on July 24, 2009 · 0 comments

This code will display all subfolders and files of whatever directory it's in. It uses the readdir() PHP function to get the directory contents, and displays each item as a link. If you want this script to work recursively to display the contents of subfolders, just save it as 'index.php' in each folder and subfolder.

$myDirectory = opendir(".");

while($thisfile = readdir($myDirectory))
{
        $dirArray[] = $thisfile;
}
closedir($myDirectory);

sort($dirArray);
for($i=0; $index< count($dirArray); $i++)
{
        echo("<a href="$dirArray[$i]">$dirArray[$i]</a>");
        //echo(" – " . filetype($dirArray[$index])); //optional
        //echo(" – " . filesize($dirArray[$index])); //optional
        echo("<br />n");
}

Previous post:

Next post: