下面的函數將給定目錄中的所有文件夾返回到多個級別。使用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