代码之家  ›  专栏  ›  技术社区  ›  Mr Coder

想使用flex在客户端显示服务器(php)的目录树吗?

  •  1
  • Mr Coder  · 技术社区  · 14 年前

    使用PHP的RecurveDirectoryIterator,我可以创建目录树,甚至可以使用RecurveIterator类展平它,但是我想创建Flex的树组件理解的目录树结构。下面是flex理解的PHP中的数组结构。

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

    请向我展示一种在服务器端将整个目录结构创建为上述数组格式的方法。 事先谢谢!!

    1 回复  |  直到 14 年前
        1
  •  3
  •   Gordon Haim Evgi    14 年前

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

    <?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
                );
            }
        }
    }