note 40745 deleted from function.readdir by tularis

From: Date: Sun, 13 May 2007 19:05:44 +0000
Subject: note 40745 deleted from function.readdir by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-126544@lists.php.net to get a copy of this message
Note Submitter: josh at duovibe dot com ---- After looking for awhile for a function which retrieves a folder listing based on certain criteria (return folder, files, or both; search recursively or not), I decided to write my own. Maybe somebody out there will find it helpful. Currently, it returns a flat list, but that can changed to return a nested list by editing the line: $file_list = array_merge($file_list, directory_list($path ... to $file_list[] = directory_list($path ... function directory_list($path, $types="directories", $recursive=0) { // get a list of just the dirs and turn the list into an array // @path starting path from which to search // @types can be "directories", "files", or "all" // @recursive determines whether subdirectories are searched if ($dir = opendir($path)) { $file_list = array(); while (false !== ($file = readdir($dir))){ // ignore the . and .. file references if ($file != '.' && $file != '..') { // check whether this is a file or directory and whether we're interested in that type if ((is_dir($path."/".$file)) && (($types=="directories") || ($types=="all"))) { $file_list[] = $file; // if we're in a directory and we want recursive behavior, call this function again if ($recursive) { $file_list = array_merge($file_list, directory_list($path."/".$file."/", $types, $recursive)); } } else if (($types == "files") || ($types == "all")) { $file_list[] = $file; } } } closedir($dir); return $file_list; } else { return false; } }

« previous php.notes (#126544) next »