note 43806 deleted from function.scandir by tomsommer
| From: | tomsommer@php.net | Date: | Sun, 04 Jul 2004 18:13:21 +0000 |
| Subject: | note 43806 deleted from function.scandir by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72541@lists.php.net to get a copy of this message | ||
Note Submitter: marcjon@fastmail.fm
----
Here's a directory scanner I wrote. It returns an array of dirs and files starting in a
specifies directory. The $max variable is used to ensure that this function doesn't overload
and crash the system.
<?php
function scan_recurse_dir ($startdir,$max = 0){
// Returns an array of files and folders.
// It starts in a dir and searches all sub dirs as well.
// Prevent double slashes.
if (substr($startdir,strlen($startdir)-1,1) == "/"){
$startdir = substr($startdir,0,strlen($startdir)-1);}
// This prevents the function from going to deep. Change as neccesary.
if ($max < 100){
$dir = $startdir;
$files1 = scandir($dir);
if (!is_dir($dir) or !is_readable($dir)){return false;}
// Loop through files and dirs.
foreach ($files1 as $file){
// If it's a folder, do a check of that folder.
if (is_dir($file) AND $file != "." AND $file != ".."){$return2 =
scan_recurse_dir ( $dir."/".$file,($max+1));
// Merge the results with results already gathered.
if ($return2) {$return = array_merge($return,$return2); unset($return2);}
}
$return[] = $dir."/".$file;
}
return $return;
} else {return "";} // If it's reached the max, don't return anything and
don't perform a scan.
}
// Example.
foreach (scan_recurse_dir("C:/inetpub/wwwroot") as $entry){echo
$entry."<br>";}
?>