我想获取所有目录名称

I need seniors help to make dropdown list of root directories using php. I created almost but having one issue is not getting root directory.

Like home/abc/ all dir I want

Code sample

     <?
$dirs = array_filter(glob('/*'), 'is_dir'); //this code getting main root folders

print_r($dirs); // array showing root directory
?>

But I want to get all directories from home/username is it possible?

Loop through user's directory

The following code finds the user directory (compatible with Windows, Mac, and Linux) then recursively echo's the directory paths.

<?php
$dir = '';
if (strpos(php_uname(),"Linux") === 0) {
  //linux
  $dir = "/home/";
} else {
  //windows and mac
  $dir = "/users/";
}
$dir.=get_current_user();
$iterator = new DirectoryIterator($dir);
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isDir() && !$fileinfo->isDot()) {
        echo $fileinfo->getPathName() . "
";
        // recursion goes here.
    }
}
?>

Looping through the entire System

The AJAX method

Ajax is basically just a way of communicating to a server without having to refresh. What the following code does it lazy loads directories. So, as it's needed, it will load the next iteration for directories. I believe this would be what you want because printing everything from the root is a bit suicidal.

<?php

  function filterDirectories($dir) {
    $myDirs = scandir($_POST['dir']);
    foreach ($myDirs as $key => $myDir) {
      $path = str_replace('//','/',$dir . '/' . $myDir);
      if (!is_dir($path) || $myDir === "." || $myDir === "..") {
        unset($myDirs[$key]);
      } else {
        $myDirs[$key] = $path;
      }
    }
    return array_values($myDirs);
  }

  if (isset($_POST['dir'])) {
    echo json_encode(filterDirectories($_POST['dir']));
    die();
  }

?>

<body>
  <form>
    <div id="selectContainer">

    </div>
  </form>
</body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>

$(function () {
  "use strict";

  var rootDir = "/";

  function getDirectory(dir) {
    $.post("#", {
      dir : dir
    }, function (data) {

      var $select = $("<select>");
      for (var i = 0, len = data.length; i < len; i++) {
        $("<option>")
          .text(data[i])
          .attr('value',data[i])
          .appendTo($select);
      }
      $("#selectContainer").append($select);

    }, "json");
  }

  getDirectory(rootDir);

  $("#selectContainer").on("change", "select", function () {
    $(this).nextAll().remove();
    getDirectory($(this).val());
  });
});

</script>

Despite the clarification to the question I'm not 100% sure the following is what you are meaning. I took it to be that you wish to get all folders under the start location - in your case /home/username

The recursiveIterator classes are very useful for this sort of task.

/* The ROOT directory you wish to scan */
$dir = 'c:/wwwroot/images';

if( realpath( $dir ) ){
    $dirs=array();

    $dirItr=new RecursiveDirectoryIterator( realpath( $dir ), RecursiveDirectoryIterator::KEY_AS_PATHNAME );

    foreach( new RecursiveIteratorIterator( $dirItr, RecursiveIteratorIterator::CHILD_FIRST ) as $file => $info ) {
        if( $info->isDir() ) $dirs[]=realpath( $info->getPathName() );
    }
    echo '<pre>',print_r( $dirs, 1 ),'</pre>';
}