FileSystemFinder: A PHP library that can list files and directories hierarchically using wildcard and regex patterns
Actually, this PHP library was created in the National Day holiday last year. But at that time, it was not as complete as it is now.

Today I cleaned up the code, made a few improvements, and commited it to github:

https://github.com/wudicgi/file-system-finder

The example is as follows:
  1. <?php
  2. include_once 'FileSystemFinder.php';
  3.  
  4. // List files using static method FileSystemFinder::find()
  5.  
  6. $filelist = FileSystemFinder::find('C:/php/ext/php_pdo_*.dll');
  7.  
  8. print_r($filelist);                 // via __debugInfo()
  9. echo "\r\n";
  10.  
  11.  
  12. // List files using file() method with a wildcard pattern
  13.  
  14. $filelist = (new FileSystemFinder('C:/php/ext'))
  15.     ->file('php_pdo_*.dll');
  16.  
  17. print_r($filelist->toArray());      // using toArray()
  18. echo "\r\n";
  19.  
  20.  
  21. // List files using dir() and file() method with wildcard and regex patterns
  22.  
  23. $filelist = (new FileSystemFinder('C:/php'))
  24.     ->dir('dev|ext')                                    // using default wildcard matcher
  25.     ->file('/[0-9]/', FileSystemFinder::REGEX_MATCHER); // using the specified regex matcher
  26.  
  27. foreach ($filelist as $path) {      // via SeekableIterator interface
  28.     echo "$path\r\n";
  29. }
  30. echo "\r\n";
  31.  
  32.  
  33. // A combination of using both static and non-static method
  34.  
  35. $filelist = FileSystemFinder::find('C:/php/dev|ext', FileSystemFinder::DIR_ONLY);
  36. print_r($filelist);
  37.  
  38. $filelist = $filelist->file('/[0-9]/', FileSystemFinder::REGEX_MATCHER);
  39. print_r($filelist);
  40.  
  41. echo "\r\n";
  42.  
  43.  
  44. // List files using wfio extension
  45.  
  46. if (extension_loaded('wfio')) {
  47.     $filelist = FileSystemFinder::find('wfio://E:/Music/* 笑话/* 欢乐剧场/??? *大*.wma');
  48.  
  49.     for ($i = 0; $i < count($filelist); $i++) {     // via Countable interface
  50.         echo "[$i] => $filelist[$i]\r\n";           // via ArrayAccess interface
  51.     }
  52. } else {
  53.     echo "The wfio extension is not loaded.\r\n";
  54. }
  55.  
  56. ?>

This will output:
FileSystemFinder Object
(
    [0] => C:/php/ext/php_pdo_firebird.dll
    [1] => C:/php/ext/php_pdo_mysql.dll
    [2] => C:/php/ext/php_pdo_oci.dll
    [3] => C:/php/ext/php_pdo_odbc.dll
    [4] => C:/php/ext/php_pdo_pgsql.dll
    [5] => C:/php/ext/php_pdo_sqlite.dll
)

Array
(
    [0] => C:/php/ext/php_pdo_firebird.dll
    [1] => C:/php/ext/php_pdo_mysql.dll
    [2] => C:/php/ext/php_pdo_oci.dll
    [3] => C:/php/ext/php_pdo_odbc.dll
    [4] => C:/php/ext/php_pdo_pgsql.dll
    [5] => C:/php/ext/php_pdo_sqlite.dll
)

C:/php/dev/php5ts.lib
C:/php/ext/php_bz2.dll
C:/php/ext/php_gd2.dll
C:/php/ext/php_oci8_12c.dll
C:/php/ext/php_sqlite3.dll

FileSystemFinder Object
(
    [0] => C:/php/dev
    [1] => C:/php/ext
)
FileSystemFinder Object
(
    [0] => C:/php/dev/php5ts.lib
    [1] => C:/php/ext/php_bz2.dll
    [2] => C:/php/ext/php_gd2.dll
    [3] => C:/php/ext/php_oci8_12c.dll
    [4] => C:/php/ext/php_sqlite3.dll
)

[0] => wfio://E:/Music/04 笑话/01 欢乐剧场/036 武大日记.wma
[1] => wfio://E:/Music/04 笑话/01 欢乐剧场/087 大学趣闻.wma
[2] => wfio://E:/Music/04 笑话/01 欢乐剧场/109 武大郎后传.wma
[3] => wfio://E:/Music/04 笑话/01 欢乐剧场/117 孙大圣“评职”申请书.wma
[4] => wfio://E:/Music/04 笑话/01 欢乐剧场/120 肖大明白.wma
[5] => wfio://E:/Music/04 笑话/01 欢乐剧场/156 吃大户.wma
[6] => wfio://E:/Music/04 笑话/01 欢乐剧场/160 说大道小.wma
[7] => wfio://E:/Music/04 笑话/01 欢乐剧场/168 四大…….wma
[8] => wfio://E:/Music/04 笑话/01 欢乐剧场/197 过大年.wma
Current language: English · 其他语言: 中文 (简体)
Leave a Comment
Name (required)
E-mail (required, will not be published)
Website (optional)
Comment
A syntax system which is similar to wiki markup is available, see the guide