note 60127 added to function.opendir

From: Date: Tue, 27 Dec 2005 15:04:14 +0000
Subject: note 60127 added to function.opendir
Groups: php.notes 
Request: Send a blank email to php-notes+get-100797@lists.php.net to get a copy of this message
<?php /* The below function will list all folders and files within a directory It is a recursive function that uses a global array. The global array was the easiest way for me to work with an array in a recursive function *This function has no limit on the number of levels down you can search. *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 $level => integer; directory level that the function is currently searching */ function filelist ($startdir="./", $searchSubdirs=1, $directoriesonly=0, $maxlevel="all", $level=1) { //list the directory/file names that you want to ignore $ignoredDirectory[] = "."; $ignoredDirectory[] = ".."; $ignoredDirectory[] = "_vti_cnf"; global $directorylist; //initialize global array if (is_dir($startdir)) { if ($dh = opendir($startdir)) { while (($file = readdir($dh)) !== false) { if (!(array_search($file,$ignoredDirectory) > -1)) { if (filetype($startdir . $file) == "dir") { //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; if ($searchSubdirs) { if ((($maxlevel) == "all") or ($maxlevel > $level)) { filelist($startdir . $file . "/", $searchSubdirs, $directoriesonly, $maxlevel, $level + 1); } } } 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; }}}} closedir($dh); }} return($directorylist); } $files = filelist("./",1,1); // call the function foreach ($files as $list) {//print array echo "Directory: " . $list['dir'] . " => Level: " . $list['level'] . " => Name: " . $list['name'] . " => Path: " . $list['path'] ."<br>"; }?> ---- Manual Page -- http://www.php.net/manual/en/function.opendir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60127 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60127&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60127&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60127&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60127&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60127&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60127&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60127&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60127&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#100797) next »