Actions

API16

JFolder/files

From Joomla! Documentation

< API16:JFolder
Revision as of 17:47, 22 March 2010 by Doxiki (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 files in a folder.

[Edit Descripton]

Description:JFolder/files

Syntax

static files($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 file 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 file.
$exclude array('.svn', 'CVS','.DS_Store','__MACOSX') Array with names of files which should not be shown in the result.
$excludefilter array('^\..*','.*~')

Returns

array Files in the given folder.

Defined in

libraries/joomla/filesystem/folder.php

Importing

jimport( 'joomla.filesystem.folder' );

Source Body

public static function files($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::files: ' . JText::_('PATH_IS_NOT_A_FOLDER'), 'Path: ' . $path);
                return false;
        }
 
        // read the source directory
        $handle = opendir($path);
        if(count($excludefilter)) {
                $excludefilter = '/('. implode('|', $excludefilter) .')/';
        } else {
                $excludefilter = '';
        }
        while (($file = readdir($handle)) !== false)
        {
                if (($file != '.') && ($file != '..') && (!in_array($file, $exclude)) && (!$excludefilter || !preg_match($excludefilter, $file))) {
                        $dir = $path . DS . $file;
                        $isDir = is_dir($dir);
                        if ($isDir) {
                                if ($recurse) {
                                        if (is_integer($recurse)) {
                                                $arr2 = JFolder::files($dir, $filter, $recurse - 1, $fullpath);
                                        } else {
                                                $arr2 = JFolder::files($dir, $filter, $recurse, $fullpath);
                                        }
 
                                        $arr = array_merge($arr, $arr2);
                                }
                        } else {
                                if (preg_match("/$filter/", $file)) {
                                        if ($fullpath) {
                                                $arr[] = $path . DS . $file;
                                        } else {
                                                $arr[] = $file;
                                        }
                                }
                        }
                }
        }
        closedir($handle);
 
        asort($arr);
        return $arr;
}

[Edit See Also] SeeAlso:JFolder/files

Examples

<CodeExamplesForm />