我想能够列出"./"文件夹中的所有目录、子目录和文件,即名为fileSystem的项目文件夹,其中包含这个php文件scanDir.php。
您可以在这里查看我拥有的目录结构:
目前,它只会返回mkdir目录根目录中的子目录文件,但不会返回该子目录中的任何文件夹。
如何修改代码以便展示fileSystem文件夹中的所有文件、目录、子目录及其文件和子目录,假设运行的php文件名为scanDir.php,其代码如下。
您可以在这里查看我拥有的目录结构:
![enter image description here](https://istack.dev59.com/TodeS.webp)
如何修改代码以便展示fileSystem文件夹中的所有文件、目录、子目录及其文件和子目录,假设运行的php文件名为scanDir.php,其代码如下。
$path = "./";
if(is_dir($path))
{
$dir_handle = opendir($path);
//extra check to see if it's a directory handle.
//loop round one directory and read all it's content.
//readdir takes optional parameter of directory handle.
//if you only scan one single directory then no need to passs in argument.
//if you are then going to scan into sub-directories the argument needs
//to be passed into readdir.
while (($dir = readdir($dir_handle))!== false)
{
if(is_dir($dir))
{
echo "is dir: " . $dir . "<br>";
if($dir == "mkdir")
{
$sub_dir_handle = opendir($dir);
while(($sub_dir = readdir($sub_dir_handle))!== false)
{
echo "--> --> contents=$sub_dir <br>";
}
}
}
elseif(is_file($dir))
{
echo "is file: " . $dir . "<br>" ;
}
}
closedir($dir_handle); //will close the automatically open dir.
}
else {
echo "is not a directory";
}
scandir
列出所有文件夹和文件,然后使用is_file
判断该项是文件还是文件夹,并重复此过程。我很快会回答完整的代码。 - undefined