Actions

API16

JFolder/folders

From Joomla! Documentation

< API16:JFolder
Revision as of 21:52, 13 May 2013 by JoomlaWikiBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Utility function to read the folders in a folder.

[<! removed edit link to red link >]

<! 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 edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />