/** * @en Find files * @ru Поиск файлов * * # listing all the files in a directory and subdirectories: * * $result = filesystem::scandir('./src/lib/'); # Array * # ( * # [0] => src/lib/include.php * # [1] => src/lib/README.md * # [2] => src/lib/src/etc/rude-system.php * # [3] => src/lib/src/etc/rude-globals.php * # [4] => src/lib/src/etc/rude-measures.php * # [5] => src/lib/src/etc/rude-visitor.php * # [6] => src/lib/src/etc/rude-date.php * # [7] => src/lib/src/etc/rude-url.php * # [8] => src/lib/src/etc/rude-html.php * # [9] => src/lib/src/etc/rude-crypt.php * # [10] => src/lib/src/etc/rude-cookies.php * # [11] => src/lib/src/etc/rude-math.php * # [12] => src/lib/src/etc/rude-autoload.php * # [13] => src/lib/src/etc/rude-headers.php * # [14] => src/lib/src/etc/rude-speedtest.php * # [15] => src/lib/src/etc/rude-curl.php * # [16] => src/lib/src/etc/rude-filesystem.php * # [17] => src/lib/src/etc/rude-session.php * # * # ... and 164 files more * # ) * * * # listing all the files in a directory and subdirectories with '.md' file extension: * * $result = filesystem::scandir('src/lib', 'md'); # Array * # ( * # [0] => src/lib/README.md * # [1] => src/lib/LICENSE.md * # [2] => src/lib/NOTICE.md * # ) * * * # listing all the files in a directory but not in subdirectories with '.php' file extension: * * $result = filesystem::search_files('src/lib', 'php', false); # Array * # ( * # [0] => include.php * # ) * * * * @param string $path * @param string $extension * @param bool $recursively Set 'true' if you need to gather files from subdirectories * * @return array */ public static function search_files($path, $extension = null, $recursively = true) { $file_list = null; if ($recursively === true) { $objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST); foreach ($objects as $name => $object) { $file_list[] = $name; } } else { $file_list = scandir($path); } $result = array(); if ($file_list) { foreach ($file_list as $file_path) { if (filesystem::is_dir($file_path)) { continue; # exclude directories } if ($extension === null) { $result[] = $file_path; continue; } if (filesystem::file_extension($file_path) == $extension) { $result[] = $file_path; } } } return $result; }