List of files and directories in php

This is a simple example to list the contents of any Directory. We will use the combination of opendir() and readdir() functions.

<?php
// initialize counter
$count = 0;
// set directory name
$dir = "D:\phpfiles";
// open directory and parse file list
if (is_dir($dir))
{
if ($dh = opendir($dir))
{
// iterate over file list
// print filenames
while (($filename = readdir($dh)) !== false)
{
if (($filename != ".") && ($filename != ".."))
{
$count++;
echo $filename . "<br>";
}
}
// close directory
closedir($dh);
}
}
echo "-- $count FILES FOUND --";
?>

Bookmark This Page

Link Partners