Plug-in PHP Chapter 5, Plug-in 24 |
(Click on the
icon to view the source for copy and pasting) |
<?php // Plug-in 24: Directory List
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
$directory = "c:\windows\\";
$result = PIPHP_DirectoryList($directory);
echo "<b>Listing:</b> $directory<br /><br />";
if ($result[0] == 0) echo "No Directories";
else
{
echo "<b>Directories:</b> ";
for ($j=0 ; $j < $result[0] ; ++$j)
echo $result[2][$j] . ", ";
}
echo "<br /><br />";
if ($result[1] == 0) echo "No files";
else
{
echo "<b>Files:</b> ";
for ($j=0 ; $j < $result[1] ; ++$j)
echo $result[3][$j] . ", ";
}
function PIPHP_DirectoryList($path)
{
// Plug-in 24: Directory List
//
// This plug-in accepts a path to a directory on the hard
// disk and returns the list of files located there. The
// function returns an array with the first element set
// to the number of subdirectories in the directory and the
// second to the number of files. If none are found then
// 0 is returned. The third and fourth elements returned
// contain arrays with all the directories and file names
// respectively. The argument required is:
//
// $path: The directory to list
$files = array();
$dirs = array();
$fnum = $dnum = 0;
if (is_dir($path))
{
$dh = opendir($path);
do
{
$item = readdir($dh);
if ($item !== FALSE && $item != "." && $item != "..")
{
if (is_dir("$path/$item")) $dirs[$dnum++] = $item;
else $files[$fnum++] = $item;
}
} while($item !== FALSE);
closedir($dh);
}
return array($dnum, $fnum, $dirs, $files);
}
?>