Core Function Glob
From Sputnik Wiki
(Difference between revisions)
(→Example) |
m (1 revision) |
(One intermediate revision by one user not shown) |
Latest revision as of 12:37, 14 June 2015
Glob( <pattern>, <flags> )
Contents |
Description
Find files/folders matching a pattern.
Parameters
pattern
The pattern. No tilde expansion or parameter substitution is done.
flags
Optional; Valid flags:
@GLOB_MARK - Adds a slash to each directory returned @GLOB_NOSORT - Return files as they appear in the directory (no sorting, directories will be at top too) @GLOB_NOCHECK - Return the search pattern if no files matching it were found @GLOB_NOESCAPE - Backslashes do not quote metacharacters @GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c' @GLOB_ONLYDIR - Return only directory entries which match the pattern @GLOB_ERR - Stop on read errors (like unreadable directories), by default errors are ignored.
Return Value
Returns an array containing the matched files/directories, an empty array if no file matched or empty array on error.
Remarks
None
Example
To search in the directory for all files and directories that end with .txt, you would write:
$filelist = glob("*.txt");
If you want a list of files and directories that begin with “te”, the code to write is:
$filelist = glob("te*");
To get a list of directories only which contain “ma”, the code is:
$filelist = glob("*ma*", @GLOB_ONLYDIR);
Another example
$dir = './'; foreach(glob($dir.'*.txt') as $file) { print( $file . "\n"); }
Also possible:
$files = glob('*.*');
This shows how to use the @GLOB_BRACE flag:
$images = Glob('images/{*.jpg,*.gif,*.png}', @GLOB_BRACE); printr($images);