Listing Specific Files in a Directory in PHP
The provided code successfully lists all files within a directory. However, to refine this list to include only files with a ".xml" or ".XML" extension, we can leverage PHP's glob() function.
glob() Function
The glob() function searches a directory for files matching a specified pattern and returns an array of the matching files. Its syntax is:
glob(pattern, flags)
where:
Matching XML Files
To list only XML files, we can use the glob() function as follows:
$files = glob('/path/to/dir/*.xml');
This pattern matches all files in the directory "/path/to/dir/" that end with ".xml". To include uppercase extensions as well, we can use:
$files = glob('/path/to/dir/*.{xml,XML}');
The resulting $files array will contain the paths to all matching XML files. You can then process or display these files as needed.
The above is the detailed content of How to list only XML files in a directory using PHP?. For more information, please follow other related articles on the PHP Chinese website!