Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

glob()

Example

Provide back an array of folders or filenames that corresponds to the given pattern:

<?php
print_r(glob(“*.txt”));
?>

What the code above might produce is:

Array (
  [0] => target.txt
  [1] => source.txt
  [2] => test.txt
  [3] => test2.txt
)

Definition and Usage

An array of filenames or directories that match a given pattern is returned by the glob() method.

Syntax

glob(patternflags)

Parameter Values

Parameter

Description

pattern

Essential. indicates the pattern to look for.

flags

Not required. outlines unique configurations.
Potential amounts

·         GLOB_MARK: Slashes each returned item

·         GLOB_NOSORT – Returns files in the directory in their unsorted state.

·         GLOB_NOESCAPE – Backslashes do not quote metacharacters GLOB_BRACE – Expands {a,b,c} to match ‘a’, ‘b’, or ‘c’

·         GLOB_ONLYDIR: Only return directories that are in line with the pattern

·         (Added in PHP 5.1) GLOB_ERR Stop on errors (by default, errors are ignored)

Technical Details

Return Value:

An array of the matched files or folders; if the pattern is not met, FALSE

PHP Version:

4.3+

PHP Changelog:

PHP 5.1: The flags parameter now includes the GLOB_ERR value.

More Examples

Example

Provide back an array of folders or filenames that corresponds to the given pattern:

<?php
print_r(glob(“*.*”));
?>

What the code above might produce is:

Array (
  [0] => contacts.csv
  [1] => default.php
  [2] => target.txt
  [3] => source.txt
  [4] => tem1.tmp
  [5] => test.htm
  [6] => test.ini
  [7] => test.php
  [8] => test.txt
  [9] => test2.txt
)