note 11503 added to readdir
| From: | daveizzard at home dot com | Date: | Thu, 22 Feb 2001 19:27:07 +0000 |
| Subject: | note 11503 added to readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-11434@lists.php.net to get a copy of this message | ||
This is a simple function to list the entire contentes of a givendirectory and all of its
subdirectories:
function GetDirContents($path,&$fileArray)
{
$currentDir=openDir("$path");
while($dirList = readDir($currentDir))
{
if(($dirList!=".")&&($dirList!=".."))
{
$i=count(&$fileArray);
$fileArray[$i] = $path."/".$dirList;
$sPath =$fileArray[$i];
if(is_dir($sPath))
{
GetDirContents($sPath,&$fileArray);
}
}
}
}
//Create a variable containing the path
//to the initial directory
$path = "some directory path";
//Create an array to hold the files
$filesArray = array();
//Pass the array by reference
GetDirContents($path,&$fileArray)
This is very useful when you need to list out the entire contents of a directory, and you need that
list for future use.
Great if you need to open and manipulate all files in a give directory and its subdirectories
without already knowing how many files and subdirectories there are.
http://www.php.net/manual/en/function.readdir.php