想要使用 FLEX 在客户端显示服务器 (PHP) 的目录树? [英] Want to show Directory Tree of server (PHP) at client side using FLEX?

查看:18
本文介绍了想要使用 FLEX 在客户端显示服务器 (PHP) 的目录树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PHP 的 RecursiveDirectoryIterator 我能够创建目录树,甚至可以使用 RecursiveIteratorIterator 类将其展平,但我想创建 flex 的 TREE 组件理解的目录树结构.下面是php中flex理解的一个数组结构.

Using RecursiveDirectoryIterator of PHP i am able to create directory tree and can even flatten it using RecursiveIteratorIterator class , but I want to create directory tree structure which TREE component of flex understands . The following is an array structure in php which flex understands.

array('label'=>'rootDirectory','children'=>array(array('label'=>'subFolder1','children'=>array(array('label'=>'file.jpg'))),array('label'=>'EmtptyFolder','children'=>array())));

请告诉我一种在服务器端将整个目录结构创建为上述数组格式的方法.提前致谢!!

Please show me a way to create the whole directory structure at server side into the above array format. Thanks in Advance !!

推荐答案

您可以根据需要调整此代码.这不是复制品&粘贴解决方案,因为我需要它用于不同的用例,但它至少应该让您从中实现自己的解决方案.关键在于调整RecursiveIteratorIterator遍历目录树时会调用的方法.

You may adjust this code to your needs. This is not a copy & paste solution as I needed it for a different UseCase, but it should get you at least halfway to implementing your own solution from it. The key lies in adjusting the methods RecursiveIteratorIterator will call when traversing the directory tree.

<?php
/**
* Prints a Directory Structure as an Nested Array
*
* This iterator can be used to wrap a RecursiveDirectoryIterator to output
* files and directories in a parseable array notation. Because the iterator
* will print the array during iteration, the output has to be buffered in
* order to be captured into a variable.
*
* <code>
* $iterator = new RecursiveDirectoryAsNestedArrayFormatter(
*     new RecursiveDirectoryIterator('/path/to/a/directory'),
*     RecursiveIteratorIterator::SELF_FIRST
* );
* ob_start();
* foreach($iterator as $output) echo $output;
* $output = ob_get_clean();
* </code>
*
*/
class RecursiveDirectoryAsNestedArrayFormatter extends RecursiveIteratorIterator
{
    /**
     * Prints one Tab Stop per current depth
     * @return void
     */
    protected function indent()
    {
        echo str_repeat("\t", $this->getDepth());
    }
    /**
     * Prints a new array declaration
     * return void
     */
    public function beginIteration()
    {
        echo 'array(', PHP_EOL;
    }
    /**
     * Prints a closing bracket and semicolon
     * @return void
     */
    public function endIteration()
    {
        echo ');', PHP_EOL;
    }
    /**
     * Prints an indented subarray with key being the current directory name
     * @return void
     */
    public function beginChildren()
    {
        $this->indent();
        printf(
            '"%s" => array(%s',
            basename(dirname($this->getInnerIterator()->key())),
            PHP_EOL
        );
    }
    /**
     * Prints a closing bracket and a comma
     * @return void
     */
    public function endChildren()
    {
        echo '),', PHP_EOL;
    }
    /**
     * Prints the indented current filename and a comma
     * @return void
     */
    public function current()
    {
        if ($this->getInnerIterator()->current()->isFile()) {
            printf(
                '%s"%s",%s',
                str_repeat("\t", $this->getDepth() +1),
                $this->getInnerIterator()->current()->getBasename(),
                PHP_EOL
            );
        }
    }
}

这篇关于想要使用 FLEX 在客户端显示服务器 (PHP) 的目录树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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