这是我的文件夹结构:
main
- folder_1
-- folder_1_1
-- folder_1_2
-- folder_1_3
- folder_2
-- folder_2_1
-- folder_2_2
-- folder_2_3
这是我的代码:
<?php
$mainfolder ="main";
function readDirs($mainfolder){
if(hasSubFolder($mainfolder)){
echo("$mainfolder HAS sub folder");
}else{
echo("$mainfolder DOESN'T HAVE sub folder");
}
}
function hasSubFolder($folder){
$newPath="";
if (is_dir($folder)) {
echo ("$folder IS a folder</br>");
$handle = opendir($folder);
while (false !== ($entry = readdir($handle))) {
$newPath = $folder."\\".$entry;
if (is_dir($newPath)){
echo "$newPath IS a folder</br>";
} else {
echo "$newPath IS NOT a folder</br>";
}
}
}
}
readDirs($mainfolder);
?>
这就是我得到的:
main IS a folder
main\. is NOT a folder
main\.. is NOT a folder
main\folder_1 is NOT a folder
main\folder_2 is NOT a folder
main DOESN'T HAVE sub folder
所以我想知道为什么“if (is_dir($newPath))”即使 $newPath 是一个文件夹/目录也不返回 true。是因为它嵌套在另一个 is_dir() 中吗?
感谢