简体   繁体   English

php浏览多个目录

[英]Php Browsing Multiple Directories

Using this code from a previous post on displaying a directory 使用上一篇文章中的代码显示目录

<?php 

      $files = array();
      $dir = opendir('races/ob/'); // open the cwd..also do an err check.

      while(false != ($file = readdir($dir))) {
              if(($file != ".") and ($file != "..") and ($file != "index.php")) {
                      $files[] = $file; // put in array.
              }   
      }

      natsort($files); // sort.

      // print.
      foreach($files as $file) {
              echo("<span class='txt-spacing'><a href='$file'>$file</a> <br />\n</>");
    }
?>

My question is how do I make the links that are generated goto the correct filepath on the server, which then open in a new page and show all the file listings which are .txt files for users to click and view the data. 我的问题是如何使生成的链接转到服务器上的正确文件路径,然后在新页面中打开该页面,并显示所有文件列表(.txt文件),供用户单击和查看数据。

My directory structure is this 我的目录结构是这个

     Races
        |
       OldBird
            |
          Clubs Name Listing
              |
            List all .txt files for users to view
function scandir_recursive($path)
{
    if ($result = scandir($path))
    {
        $scan = $result = array_filter($result, function ($a) { return !in_array($a, ['.', '..', 'index.php']); });
        foreach ($scan as $sub)
            if (is_dir($subdir = "$path/$sub"))
                if ($dir = scandir_recursive($subdir))
                    $result = array_merge($result, array_map(function ($a) use ($sub) { return "$sub/$a"; }, $dir));
    }
    return $result;
}

$files = scandir_recursive('races/ob');
natsort($files);
foreach ($files as $file)
    echo("<a class='txt-spacing' href='$file'>$file</a><br>\n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM