递归读取目录内容

$path='E:/wamp/phplianxi/';
$nested_list = readDirSNested($path, 0);
echo '<pre>';
var_dump($nested_list);
/**
 * 递归读取目录内容
 * @param string $path 需要读取的目录内容
 * @return void
 */
function readDirSNested($path) {
 $nested = array();//存储当前目录下所有内容
 $handle = opendir($path);
 while(false !== ($file_name=readdir($handle))) {
  if ($file_name == '.' || $file_name == '..') continue;
  $file_info = array();//存储当前文件信息的数组
  $file_info['name'] = $file_name;

  // 判断当前文件是否为目录,如果是,递归调用该函数完成该目录的内容获取
  if (is_dir($path . '/' . $file_name)) {
   $file_info['type'] = 'dir';
   // 目录, 递归
   $func_name = __FUNCTION__;// 魔术常量,表示当前函数名
   $file_info['nested'] = $func_name($path . '/' . $file_name);
  } else {
   //文件
   $file_info['type'] = 'file';
  }
  $nested[] = $file_info;
 }
 closedir($handle);
 return $nested;
}

原文地址:https://www.cnblogs.com/love1226/p/4499191.html