我正在尝试创建一个函数,该函数扫描文件夹中的子文件夹,然后返回一个包含这些文件夹名称的数字数组。
这是我用来测试的代码。一旦我让它打印出文件夹名称,而不仅仅是“。和"..“现在和上面的文件夹都会很好,我可以完成功能。
<?php
function super_l_getthemes($dir="themes")
{
if ($handle = opendir($dir)) {
echo "Handle: {$handle}\n";
echo "Files:\n";
while (false !== ($file = readdir($handle))) {
echo "{$file}<br>";
}
closedir($handle);
}
?>上面的代码运行良好,可以打印出文件夹的所有内容:文件、子文件夹和“。和"..“
但如果我换成:
while (false !== ($file = readdir($handle))) {
echo "{$file}<br>";
}通过以下方式:
while (false !== ($file = readdir($handle))) {
if(file_exists($file) && is_dir($file)){echo "{$file}";}
}该函数仅打印“。和"..“,而不是我想要打印的两个文件夹名称。
任何帮助都是非常感谢的。
发布于 2010-06-08 22:30:46
如果您只需要起始文件夹的目录,您可以简单地执行以下操作:
glob('/some/path/to/search/in/*', GLOB_ONLYDIR);它只会给你数组中的那些文件夹名称。如果希望所有目录都位于给定路径下,可以尝试使用SPL's RecursiveDirectoryIterator
$fileSystemIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('/some/path/to/look/in'),
RecursiveIteratorIterator::SELF_FIRST);迭代器可以与foreach一起使用
$directories = array();
foreach($fileSystemIterator as $path => $fileSystemObject) {
if($fileSystemObject->isDir()) {
$directories[] = $path;
}
}然后,您将拥有一个数组$directories,其中包含给定路径下的所有目录。
https://stackoverflow.com/questions/2997991
复制相似问题