JFolder/files
From Joomla! Documentation
< API16:JFolder
The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
Description[edit]
Utility function to read the files in a folder.
<! removed transcluded page call, red link never existed >
Syntax[edit]
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[edit]
array Files in the given folder.
Defined in[edit]
libraries/joomla/filesystem/folder.php
Importing[edit]
jimport( 'joomla.filesystem.folder' );
Source Body[edit]
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;
}
<! removed transcluded page call, red link never existed >
Examples[edit]
Code Examples[edit]