沃梦达 / 编程问答 / php问题 / 正文

如何在没有 javascript 的情况下使用 php 列出目录以浏览文件夹?

How do I list a directory with php to navigate through folders, without javascript?(如何在没有 javascript 的情况下使用 php 列出目录以浏览文件夹?)

本文介绍了如何在没有 javascript 的情况下使用 php 列出目录以浏览文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找这个 PHP 函数:

  • 列出目录,递归
  • 浏览文件夹的功能
  • 没有 JavaScript
  • 通过网络地址、?p=2|1"或类似内容导航
  • 按类型排序,然后按名称排序

解决方案

root = realpath($root);如果 ($active !== null) {$this->active = realpath($this->root .'/' . $active);}}公共函数 isActive($element) {return substr($this->active, 0, strlen($element->getPathname())) === $element->getPathname();}公共函数 getLink($element) {返回 '​​?'.http_build_query(数组(self::URL_KEY =>substr($element->getPathname(), strlen($this->root))));}受保护的函数 _get(Iterator $it) {$result = array();$dirs = $files = array();foreach ($it as $entry) {如果 ($entry->isDir()) {$data = (object)array('类型' =>'目录','名称' =>$entry->getBasename(),'对象' =>$entry);如果 ($this->isActive($entry)) {$data->children = $this->_get($it->getChildren());$data->active = true;}$dirs[$entry->getBasename()] = $data;}别的 {$files[$entry->getFilename()] = (object)array('类型' =>'文件','名称' =>$entry->getFilename(),'对象' =>$条目,'活跃' =>$this->isActive($entry));}}uksort($dirs, 'strnatcmp');uksort($files, 'strnatcmp');返回 array_values(array_merge($dirs, $files));}公共函数 get() {返回 $this->_get(new RecursiveDirectoryIterator($this->root));}公共函数 outputUl($dirTree = null) {如果($dirTree === null){$dirTree = $this->get();}echo '
    ';foreach ($dirTree 作为 $element) {$classes = array($element->type);if ($element->type === 'dir') {如果 ($element->active) {$classes[] = '活动';}echo '<li class="', implode(' ', $classes), '">';echo 'getLink($element->object),'">';echo $element->name;回声'</a>';如果 (sizeof($element->children) > 0) {$this->outputUl($element->children);}echo '</li>';}别的 {如果 ($element->active) {$classes[] = '活动';}echo '<li class="', implode(' ', $classes), '">';echo 'getLink($element->object),'">';echo $element->name;回声'</a>';echo '</li>';}}回声'</ul>';}}?><!DOCTYPE html><头><meta charset="utf-8"><title>目录树</title><style type="text/css">#dirTree a {文字装饰:无;颜色:#171717;}#dirTree .file a {颜色:#999999;}#dirTree .active >一个 {字体粗细:粗体;}</风格><身体><div id="dirTree"><?php$dirTree = 新目录树('.',isset($_GET[DirTree::URL_KEY]) ?$_GET[DirTree::URL_KEY] : null);$dirTree->outputUl();?>

I am looking for this PHP function:

  • List a directory, recursive
  • The functionallity to navigate through folders
  • No javascript
  • Navigation via web adress, "?p=2|1" or something like that
  • Sorted by type then names

解决方案

<?php
class DirTree
{
    protected $root;
    protected $active;
    const URL_KEY = 'el';

    public function __construct($root, $active = null) {
        $this->root = realpath($root);

        if ($active !== null) {
            $this->active = realpath($this->root . '/' . $active);
        }
    }

    public function isActive($element) {
        return substr($this->active, 0, strlen($element->getPathname())) === $element->getPathname();
    }

    public function getLink($element) {
        return '?' . http_build_query(array(
            self::URL_KEY => substr($element->getPathname(), strlen($this->root))
        ));
    }

    protected function _get(Iterator $it) {
        $result = array();

        $dirs = $files = array();
        foreach ($it as $entry) {
            if ($entry->isDir()) {
                $data = (object)array(
                    'type' => 'dir',
                    'name' => $entry->getBasename(),
                    'object' => $entry
                );

                if ($this->isActive($entry)) {
                    $data->children = $this->_get($it->getChildren());
                    $data->active = true;
                }

                $dirs[$entry->getBasename()] = $data;
            }
            else {
                $files[$entry->getFilename()] = (object)array(
                    'type' => 'file',
                    'name' => $entry->getFilename(),
                    'object' => $entry,
                    'active' => $this->isActive($entry)
                );
            }
        }

        uksort($dirs, 'strnatcmp');
        uksort($files, 'strnatcmp');

        return array_values(array_merge($dirs, $files));
    }

    public function get() {
        return $this->_get(
            new RecursiveDirectoryIterator($this->root)
        );
    }

    public function outputUl($dirTree = null) {
        if ($dirTree === null) {
            $dirTree = $this->get();
        }

        echo '<ul>';
        foreach ($dirTree as $element) {
            $classes = array($element->type);

            if ($element->type === 'dir') {
                if ($element->active) {
                    $classes[] = 'active';
                }

                echo '<li class="', implode(' ', $classes), '">';
                echo '<a href="', $this->getLink($element->object),'">';
                echo $element->name;
                echo '</a>';
                if (sizeof($element->children) > 0) {
                    $this->outputUl($element->children);
                }
                echo '</li>';
            }
            else {
                if ($element->active) {
                    $classes[] = 'active';
                }           

                echo '<li class="', implode(' ', $classes), '">';
                echo '<a href="', $this->getLink($element->object),'">';
                echo $element->name;
                echo '</a>';
                echo '</li>';
            }
        }

        echo '</ul>';
    }
}
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>DirTree</title>
        <style type="text/css">
            #dirTree a {
                text-decoration: none;
                color: #171717;
            }

            #dirTree .file a {
                color: #999999;
            }

            #dirTree .active > a {
                font-weight: bold;
            }
        </style>
    </head>

    <body>
        <div id="dirTree">
            <?php
                $dirTree = new DirTree(
                    '.',
                     isset($_GET[DirTree::URL_KEY]) ? $_GET[DirTree::URL_KEY] : null
                );
                $dirTree->outputUl();
            ?>
        </div>
    </body>
</html>

这篇关于如何在没有 javascript 的情况下使用 php 列出目录以浏览文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在没有 javascript 的情况下使用 php 列出目录以浏览文件夹?

基础教程推荐