【发布时间】:2016-01-16 06:51:14
【问题描述】:
我希望能够列出“./”文件夹中的所有目录、子目录和文件,即名为 fileSystem 的项目文件夹,其中包含这个 php 文件 scanDir.php。
此时它只会返回 mkdir 目录根目录中的子目录文件夹/文件,但不会返回该子目录中的任何文件夹。
如果正在运行的 php 文件名为 scanDir.php 并且下面提供了代码,我该如何修改代码,以便它演示 fileSystem 文件夹中的所有文件、目录、子目录及其文件和子目录。 这是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项目是文件,否则是文件夹并重复该过程。我很快就会用完整的代码来回答 -
谢谢,期待看到你的完整代码
标签: php file directory subdirectory