note 59965 added to function.opendir
| From: | mstabile75 at gmail dot com | Date: | Wed, 21 Dec 2005 13:53:46 +0000 |
| Subject: | note 59965 added to function.opendir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100515@lists.php.net to get a copy of this message | ||
<?php
/* The below function will list all folders and files with in a directory
It is a function with a recursive subfunction within. The reason I used a subfunction is because
I did not know how to properly pass variables within a recursive function.
*This function has no limit on the number of levels down you can search.
*This was the cleanest method I could come up with.
*The array structure was one that worked for me.
ARGUMENTS:
$startdir => specify the directory to start from; format: must end in a "/"
$searchSubdirs => True/false; True if you want to search subdirectories
$directoriesonly => True/false; True if you want to only return directories
$maxlevel => "all" or a number; specifes the number of directories down that you want
to search
*/
function filelist($startdir="./", $searchSubdirs=1, $directoriesonly=0,
$maxlevel="all") {
global $ignoredDirectory, $directorylist;
//list the directory names that you want to ignore
$ignoredDirectory[] = "."; $ignoredDirectory[] = ".."; $ignoredDirectory[] =
"_vti_cnf";
$directorylist = array(); //define $directorylist to be used by function filesearch
function filesearch ($startdir="./", $searchSubdirs=1, $directoriesonly=0, $level=1,
$maxlevel) {
//subfuntion that searched through the directories
global $ignoredDirectory, $directorylist; //initialize globals in subfunction
if (is_dir($startdir)) { //if the directory exists start
if ($dh = opendir($startdir)) { //if you can open the directory continue
while (($file = readdir($dh)) !== false) { //while there are items in the
directory
if (!(array_search($file,$ignoredDirectory) > -1)) {
//test to see if it is one of the ignored directories
$parentarray = explode("/",$startdir);
//these 2 lines will parse out the parent folder
$parent = $parentarray[(count($parentarray)-2)];
if (filetype($startdir . $file) == "dir") {
//the current item is a directory
//build your directory array however you choose; add other file details that you
want.
$directorylist[$startdir . $file]['level'] = $level;
$directorylist[$startdir . $file]['dir'] = 1; $directorylist[$startdir .
$file]['name'] = $file;
$directorylist[$startdir . $file]['path'] = $startdir;
$directorylist[$startdir . $file]['parent'] = $parent;
if ($searchSubdirs) {
if ((($maxlevel) == "all") or ($maxlevel > $level)) {
filesearch($startdir . $file . "/", $searchSubdirs, $directoriesonly,
$level + 1, $maxlevel);
}
}
} else {
if (!$directoriesonly) { //if you want to include files
//build your file array however you choose; add other file details that you want.
$directorylist[$startdir . $file]['level'] = $level;
$directorylist[$startdir . $file]['dir'] = 0;
$directorylist[$startdir . $file]['name'] = $file;
$directorylist[$startdir . $file]['path'] = $startdir;
$directorylist[$startdir . $file]['parent'] = $parent;
}}}}
closedir($dh);
}}}
filesearch($startdir, $searchSubdirs, $directoriesonly,1, $maxlevel);
return($directorylist);
}
// call the function
$files = filelist("./",1,0,4);
foreach ($files as $list) {//print array
echo key($files) . "<br>"; //the key is the fullpath and filename. For some reason
this "echo" adds "Array" at the beginning of the key
echo "Directory: " . $list['dir'] .
" Parent: " . $list['parent'] .
" Level: " . $list['level'] .
" Name: " . $list['name'] .
" Path: " . $list['path']
."<br>";
echo next($files);
}?>
----
Manual Page -- http://www.php.net/manual/en/function.opendir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59965
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59965&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59965&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59965&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59965&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59965&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59965&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59965&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59965&report=yes
Search -- http://master.php.net/manage/user-notes.php