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

查看:33
本文介绍了如何在没有 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 '

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 列出目录以浏览文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆