php glob-在子文件夹中扫描文件

php glob-在子文件夹中扫描文件,第1张

php glob-在子文件夹中扫描文件

有两种方法。

glob
递归搜索:

<?php// Does not support flag GLOB_BRACEfunction rglob($pattern, $flags = 0) {    $files = glob($pattern, $flags);     foreach (glob(dirname($pattern).'/*', GLOB_onLYDIR|GLOB_NOSORT) as $dir) {        $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));    }    return $files;}?>

采用

RecursiveDirectoryIterator

<?phpfunction rsearch($folder, $pattern) {    $dir = new RecursiveDirectoryIterator($folder);    $ite = new RecursiveIteratorIterator($dir);    $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);    $fileList = array();    foreach($files as $file) {        $fileList = array_merge($fileList, $file);    }    return $fileList;}?>

RecursiveDirectoryIterator
来自PHP5,而
glob
来自PHP4。两者都能胜任,这取决于您。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5023586.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存