note 59965 deleted from function.opendir by nlopess

From: Date: Thu, 22 Dec 2005 01:18:56 +0000
Subject: note 59965 deleted from function.opendir by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-100555@lists.php.net to get a copy of this message
Note Submitter: mstabile75 at gmail dot com ---- <?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'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Parent: " . $list['parent'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Level: " . $list['level'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name: " . $list['name'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Path: " . $list['path'] ."<br>"; echo next($files); }?>

« previous php.notes (#100555) next »