10.24 2017-11-18 08:50 采纳率: 0%
浏览 67

我想获取所有目录名称

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?

  • 写回答

2条回答 默认 最新

  • Lotus@ 2017-11-18 09:21
    关注

    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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记