下面的函数将给定目录中的所有文件夹返回到多个级别。使用glob返回给定目录中的文件夹列表(不含路径)
我只需要一个级别的深度,只是目标目录中的文件夹,没有子文件夹。
另外该函数返回文件夹的完整路径,我只想要文件夹名称。我确定我错过了一些简单的东西。
如何修改函数以仅返回给定目录的文件夹名称? (不是每个文件夹的完整路径)
$ myArray = get_dirs('../ wp-content/themes/mytheme/images');
<?php
function get_dirs($path = '.'){
return glob(
'{' .
$path . '/*,' . # Current Dir
$path . '/*/*,' . # One Level Down
$path . '/*/*/*' . # Two Levels Down, etc.
'}', GLOB_BRACE + GLOB_ONLYDIR);
}
?>
btw,感谢Doug对原函数的帮助!
嗨乔丹,这看起来不错,但我有一个问题。我从本地运行它,但得到一个致命的错误... 致命错误:未知的异常'UnexpectedValueException'消息'DirectoryIterator :: __构造(../ wp-content/themes/mytheme/images)[directoryiterator.--construct]:无法打开目录:没有这样的文件或目录'在C:\ xampplite \ htdocs \ wordpress \ wp-content \ themes \ mytheme \ functions.php:436 – 2009-12-15 17:18:21
这是我的调用者。 $ mydir = get_dirs('../ wp-content/themes/mytheme/images'); – 2009-12-15 17:19:00
尝试'$ mydir = get_dirs(realpath('../ wp-content/themes/mytheme/images'));'而不是。 – 2009-12-15 22:17:06