本文介绍了仅搜索和列出特定目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想搜索和列出特定的文件夹,无论这些文件夹的存放深度如何.
I want to search and list specific folders only and no matter how deep these folders are kept.
例如,下面是我的结构方式,
For instance, below is how I structure them,
local/
app/
master/
models/
views/
slaves/
models/
views/
scr/
models/
index.php
我只想将models
的文件夹列出到一个数组中,
And I just want to list the folder of models
into an array,
local/app/master/models/
local/app/slaves/models/
local/models/
我的工作代码,
$directories = array();
$results = array_diff( scandir("local"), array(".", "..") );
foreach ($results as $result)
{
if (is_dir("local/".$result)) {
$directories[] = $result;
}
}
var_dump($directories);
结果
array
0 => string 'app' (length=3)
1 => string 'models' (length=6)
2 => string 'src' (length=3)
有什么想法吗?
推荐答案
// Create an object that allows us to iterate directories recursively
// Stolen from here:
// http://www.php.net/manual/en/class.recursivedirectoryiterator.php#102587
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::CHILD_FIRST);
// This will hold the result
$result = array();
// Loop the directory contents
foreach ($iterator as $path) {
// If object is a directory and matches the search term ('models')...
if ($path->isDir() && $path->getBasename() === 'models') {
// Add it to the result array
$result[] = (string) $path;
}
}
print_r($result);
这篇关于仅搜索和列出特定目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!