glob() function in PHP

PHPProgrammingServer Side Programming

The glob() function returns an array of filenames or directories matching a specified pattern. The glob() function returns.

  • An array containing the matched files/directories,
  • Returns an empty array if no file is matched,
  • FALSE on error.

Syntax

glob(pattern,flags)

Parameters

  • pattern − The pattern to search for.

  • flags − The following are the flags:

    • GLOB_MARK - Adds a slash to each item returned
    • GLOB_NOSORT - Return files as they appear in the directory (unsorted)
    • GLOB_NOCHECK - Returns the search pattern if no match were found
    • GLOB_NOESCAPE - Backslashes do not quote metacharacters
    • GLOB_BRACE - Expands {p,q,r} to match 'p', 'q', or 'r'
    • GLOB_ONLYDIR - Return only directories which match the pattern
    • GLOB_ERR - Stop on errors. The errors are ignored by default.

Return

The glob() function returns an array containing the matched files/directories. Returns an empty array if no file is matched and FALSE on error.

Example

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

Output

Array
(
   [0] => one.htm
   [1] => two.htm
   [2] => three.htm
)

Let us see another example.

Example

<?php
   foreach (glob("*.*") as $myfiles) {
      echo "$myfiles filesize = " . filesize($myfiles) . "\n";
   }
?>

Output

one.htm filesize = 56790
two.htm filesize = 432987
new.docx filesize = 184256
students.csv filesize = 4626
php.int filesize = 1287
settings.ini filesize = 3516
raja
Updated on 24-Jun-2020 08:57:19

Advertisements