<pre name="code" class="php"><?php
/**
* @param $path 需要读取的目录内容
*/
function readDirs($path, $deep=0) {
//打开,读取
$handle = openDir($path);
//循环获得文件
while(false !== $file = readDir($handle)) {
//是不是伪目录 ., ..,是的话不处理
if ($file == '.' || $file == '..') continue;
echo str_repeat(' ', $deep*4), $file,'<br>';
//判断该文件是否为目录
if(is_dir($path . '/' . $file)) {
//是目录,递归的获取
readDirs($path . '/' . $file, $deep+1);
}
}
closeDir($handle);
}
将获取的目录保存起来,以便之后使用代码如下
/**
* @param $path 需要读取的目录内容
*
* @return array 很多维数组
*/
function readDirs($path, $deep=0) {
$children = array();
//打开,读取
$handle = openDir($path);
//循环获得文件
while(false !== $file = readDir($handle)) {
//是不是伪目录 ., ..,是的话不处理
if ($file == '.' || $file == '..') continue;
//记录当前文件信息的数组
$file_info['name']=$file;//文件名
//判断该文件是否为目录
if(is_dir($path . '/' . $file)) {
//是目录,递归的获取
$file_info['type'] = 'dir';
$file_info['children'] = readDirs($path . '/' . $file, $deep+1);
} else {
$file_info['type'] = 'file';
}
$children[] = $file_info;
}
closeDir($handle);
return $children;
}
/**
* @param $path 删除需要目录
*/
function rmDirs($path) {
//打开,读取
$handle = openDir($path);
//循环获得文件
while(false !== $file = readDir($handle)) {
//是不是伪目录 ., ..,是的话不处理
if ($file == '.' || $file == '..') continue;
//判断该文件是否为目录
if(is_dir($path . '/' . $file)) {
//是目录,递归的获取
rmDirs($path . '/' . $file);
} else {
//文件
unlink($path . '/' . $file);//unlink删除文件
}
}
closeDir($handle);
return rmdir($path);
}