Core Function Glob

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> Glob( <pattern>, <flags> ) </pre> === Description === Find files/folders matching a pattern. === Parameters === ==== pattern ==== The pattern. No tilde expansion or pa...")
 
m (1 revision)
 
(4 intermediate revisions by one user not shown)
Line 37: Line 37:
  
 
To search in the directory for all files and directories that end with .txt, you would write:
 
To search in the directory for all files and directories that end with .txt, you would write:
<syntaxhighlight lang="sputnik
+
<syntaxhighlight lang="sputnik">
 
$filelist = glob("*.txt");
 
$filelist = glob("*.txt");
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
If you want a list of files and directories that begin with “te”, the code to write is:
 
If you want a list of files and directories that begin with “te”, the code to write is:
<syntaxhighlight lang="sputnik
+
<syntaxhighlight lang="sputnik">
 
$filelist = glob("te*");
 
$filelist = glob("te*");
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
To get a list of directories only which contain “ma”, the code is:
 
To get a list of directories only which contain “ma”, the code is:
<syntaxhighlight lang="sputnik
+
<syntaxhighlight lang="sputnik">
 
$filelist = glob("*ma*", @GLOB_ONLYDIR);
 
$filelist = glob("*ma*", @GLOB_ONLYDIR);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
Another example
 
Another example
<syntaxhighlight lang="sputnik
+
<syntaxhighlight lang="sputnik">
 
$dir = './';
 
$dir = './';
 
   
 
   
Line 62: Line 62:
  
 
Also possible:
 
Also possible:
<syntaxhighlight lang="sputnik
+
<syntaxhighlight lang="sputnik">
 
$files = glob('*.*');
 
$files = glob('*.*');
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 68: Line 68:
 
This shows how to use the @GLOB_BRACE flag:
 
This shows how to use the @GLOB_BRACE flag:
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$images = Glob("images/{*.jpg,*.gif,*.png}", @GLOB_BRACE);
+
$images = Glob('images/{*.jpg,*.gif,*.png}', @GLOB_BRACE);
 
printr($images);
 
printr($images);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

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);
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox