PHP Code

Display Directory Contents with PHP

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;

for($i=0; $index< count($dirArray); $i++)
	echo("<a href=\"$dirArray&#91;$i&#93;\">$dirArray[$i]</a>");
	//echo(" - " . filetype($dirArray[$index])); //optional
	//echo(" - " . filesize($dirArray[$index])); //optional
	echo("<br />\n");

Leave a Reply

Your email address will not be published. Required fields are marked *

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image