In a previous article, we explored how to list all files in a directory using PHP. While this code is straightforward, it may not always be suitable when you need to filter and display specific files.
To list only files with a particular file extension (such as ".xml"), we can utilize PHP's glob() function. This function takes a pattern as an argument and returns an array of filenames that match the specified pattern.
The following code snippet demonstrates how to list XML files in a directory:
$files = glob('/path/to/dir/*.xml'); foreach ($files as $file) { echo $file . PHP_EOL; }
In this code:
The above is the detailed content of How to List Specific Files in a PHP Directory Using glob()?. For more information, please follow other related articles on the PHP Chinese website!