API16:JFolder/folders
From Joomla! Documentation
Description
Utility function to read the folders in a folder.
<! removed transcluded page call, red link never existed >
Syntax
static folders($path, $filter= '.', $recurse=false, $fullpath=false, $exclude=array('.svn', 'CVS','.DS_Store','__MACOSX'), $excludefilter=array('^\..*'))
| Parameter Name | Default Value | Description |
|---|---|---|
| $path | The path of the folder to read. | |
| $filter | '.' | A filter for folder names. |
| $recurse | false | True to recursively search into sub-folders, or an integer to specify the maximum depth. |
| $fullpath | false | True to return the full path to the folders. |
| $exclude | array('.svn', 'CVS','.DS_Store','__MACOSX') | Array with names of folders which should not be shown in the result. |
| $excludefilter | array('^\..*') | Array with regular expressions matching folders which should not be shown in the result. |
Returns
array Folders in the given folder.
Defined in
libraries/joomla/filesystem/folder.php
Importing
jimport( 'joomla.filesystem.folder' );
Source Body
public static function folders($path, $filter = '.', $recurse = false, $fullpath = false, $exclude = array('.svn', 'CVS','.DS_Store','__MACOSX'), $excludefilter = array('^\..*'))
{
// Initialise variables.
$arr = array();
// Check to make sure the path valid and clean
$path = JPath::clean($path);
// Is the path a folder?
if (!is_dir($path)) {
JError::raiseWarning(21, 'JFolder::folder: ' . JText::_('PATH_IS_NOT_A_FOLDER'), 'Path: ' . $path);
return false;
}
// read the source directory
$handle = opendir($path);
if(count($excludefilter)) {
$excludefilter_string = '/('. implode('|', $excludefilter) .')/';
} else {
$excludefilter_string = '';
}
while (($file = readdir($handle)) !== false)
{
if (($file != '.') && ($file != '..') && (!in_array($file, $exclude)) && (empty($excludefilter_string) || !preg_match($excludefilter_string, $file))) {
$dir = $path . DS . $file;
$isDir = is_dir($dir);
if ($isDir) {
// Removes filtered directories
if (preg_match("/$filter/", $file)) {
if ($fullpath) {
$arr[] = $dir;
} else {
$arr[] = $file;
}
}
if ($recurse) {
if (is_integer($recurse)) {
$arr2 = JFolder::folders($dir, $filter, $recurse - 1, $fullpath, $exclude, $excludefilter);
} else {
$arr2 = JFolder::folders($dir, $filter, $recurse, $fullpath, $exclude, $excludefilter);
}
$arr = array_merge($arr, $arr2);
}
}
}
}
closedir($handle);
asort($arr);
return $arr;
}
<! removed transcluded page call, red link never existed >
Examples
Code Examples