PHP fnmatch() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fnmatch() checks whether a file name matches a shell style pattern, such as *.txt.

It uses the same wildcard rules as the command line. A star matches any text, a question mark matches one character, and square brackets match one character from a set.

Syntax

Call fnmatch() with the values you need. Start with this line.

PHP
$match = fnmatch('*.txt', 'notes.txt');
CallWhat it does
fnmatch('*.txt', 'notes.txt')Returns true because the name ends with .txt.
fnmatch('file?.log', 'file1.log')Returns true because the question mark matches the single character 1.
fnmatch('img[0-9].png', 'img7.png')Returns true because 7 is in the range 0 to 9.

The pattern is not a regular expression. If you need folders and subfolders, glob() can search the disk for you, while fnmatch() only tests a name you already have. On very old Windows builds of PHP the function may not be available.

Library declarations
PHP
fnmatch(string $pattern, string $filename, int $flags = 0): bool

Parameters

ParameterDescription
patternRequired. The shell wildcard pattern to test against, for example *.php.
filenameRequired. The string to check. It does not need to be a real file.
flagsOptional. A combination of FNM_NOESCAPE, FNM_PATHNAME, FNM_PERIOD and FNM_CASEFOLD. The default is 0.

Return Value

Return ValueDescription
boolReturns true if the name matches the pattern, otherwise false.

PHP Examples

Example 1 — Match a File Extension

Check whether a file name ends with .txt.

PHP
<?php
$name = 'notes.txt';

if (fnmatch('*.txt', $name)) {
    echo "$name is a text file", PHP_EOL;
} else {
    echo "$name is not a text file", PHP_EOL;
}

How It Works

  1. The pattern *. txt means any text followed by .txt.
  2. fnmatch() compares the pattern with notes. txt and finds a match.
  3. It returns true, so the first branch runs.
  4. Nothing is read from the disk, because only the name is tested.

Example 2 — Ignore the Letter Case

The match is case sensitive by default. Add FNM_CASEFOLD to treat JPG and jpg the same.

PHP
<?php
$name = 'photo.JPG';

var_dump(fnmatch('*.jpg', $name));
var_dump(fnmatch('*.jpg', $name, FNM_CASEFOLD));

How It Works

  1. The pattern uses lower case jpg, but the name ends with upper case JPG.
  2. Without a flag, the letters must match exactly, so the result is false.
  3. FNM_CASEFOLD tells fnmatch() to ignore the case.
  4. The second call returns true.

Example 3 — Filter a List of Names

Loop through a list and keep only the names that match a pattern.

PHP
<?php
$files = ['index.php', 'style.css', 'app.php', 'readme.md'];

foreach ($files as $file) {
    if (fnmatch('*.php', $file)) {
        echo $file, PHP_EOL;
    }
}

How It Works

  1. The $files array holds four example names.
  2. foreach checks each name with the pattern *. php.
  3. Only index. php and app.php end with .php, so only they are printed.
  4. style. css and readme.md are skipped because the pattern does not match them.

Frequently Asked Questions

It checks whether a string matches a shell wildcard pattern and returns true or false.
fnmatch() tests one name against a pattern. glob() searches the disk and returns all the files that match a pattern.
No. It only uses simple wildcards like *, ? and [abc]. For complex rules, use preg_match().
Pass FNM_CASEFOLD as the third argument.
No. It only compares text, so the name does not need to exist on the disk.

Next: fopen()

fopen() opens a file or a URL and gives you a handle that other functions, such as fgets().

fopen() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful