note 43806 added to function.scandir
| From: | marcjon at fastmail dot fm | Date: | Sun, 04 Jul 2004 17:59:39 +0000 |
| Subject: | note 43806 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72540@lists.php.net to get a copy of this message | ||
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>";}
?>
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43806
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43806&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43806&report=yes
Search -- http://master.php.net/manage/user-notes.php