Php glob

From CoderLearner
Jump to: navigation, search

PHP function glob

This function returns a matched list as an array based on a specific pattern.

Syntax glob

  • pattern: a string match pattern
  • flags: sets rule for the match
glob(pattern, flags);

Flags

  • GLOB_MARK - Adds a slash to each directory returned
  • GLOB_NOSORT - Return files as they appear in the directory (no sorting)
  • 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.

Note

Example 1

<?php 
   print_r(glob("*.php"));
?>

Output

These are the files in the current directory.

Array
(
    [0] => fileinode.php
    [1] => filemtime.php
    [2] => fileowner.php
    [3] => fileperms.php
    [4] => filesize.php
    [5] => filetype.php
    [6] => flock2.php
    [7] => glob.php
    [8] => is_write.php
    [9] => tempnam.php
    [10] => tmpfile.php
    [11] => touch.php
    [12] => umask.php
    [13] => umask2.php
)

Example 2

<?php 
   print_r(glob("/home/abc/*.csv"));
?>