Home >Backend Development >PHP Tutorial >PHP glob() function

PHP glob() function

不言
不言Original
2018-04-16 13:59:322343browse

This article mainly introduces the PHP glob() function, which has certain reference value. Now I share it with you. Friends in need can refer to it

PHP glob() function


PHP Filesystem function


Definition and usage


##glob() function returns the file name or directory that matches the specified pattern.

This function returns an array containing matching files/directories. Returns false if an error occurs.

Syntax

glob(pattern,flags)

ParametersDescriptionpattern Required. Specifies the search mode. flags
Optional. Specifies special settings.

  • GLOB_MARK - Add a slash to each returned item

  • GLOB_NOSORT - Return the files in their original order of appearance in the directory ( No sorting)

  • GLOB_NOCHECK - Returns the pattern used to search if no files match

  • GLOB_NOESCAPE - Backslash does not escape metacharacters

  • GLOB_BRACE - expands {a,b,c} to match 'a', 'b' or 'c'

  • GLOB_ONLYDIR - only Returns directory entries matching the pattern

  • GLOB_ERR - Stops and reads error messages (e.g. unreadable directories), ignores all errors by default

Note: GLOB_ERR was added in PHP 5.1.


##Example

Example 1

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


Output is similar:

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

Example 2

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

Output is similar:

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
)

Related recommendations:

const and global in php

The above is the detailed content of PHP glob() function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn