【问题标题】:Creating filetree from full path in php在php中从完整路径创建文件树
【发布时间】:2014-05-27 12:29:05
【问题描述】:

我正在尝试从完整路径字符串创建某种文件树。

这是我使用的 php-class 给我的:

/RootFolder/Folder1/File1.doc
/RootFolder/Folder1/SubFolder1/File1.txt
/RootFolder/Folder2/SubFolder1/File2.txt
/RootFolder/Folder2/SubFolder1/SubSubFolder1/File4.doc

我想我可以制作一个像这样的数组:

array 
(
 "RootFolder" => array 
                ("Folder1" => array
                             ("FILE" => "File1.doc"
                              "SubFolder1" => "File1.txt"
                              "SubFolder1" => "File2.txt"                                  
                             )
                 "Folder2" => array
                             (
                              ...
                              )
                 )             
  )

使用<li><table> 最终得到这个结果:

RootFolder/
     |_ Folder1/
     |        |_ SubFolder1/
     |        |           |_ File1.txt
     |        |           |_ File2.txt
     |        |_ File1.doc
     |
     |_ Folder2/
              |_ SubFolder1/
                          |_ SubSubFolder1/
                                         |_ File2.txt

我坚持这个是因为我有点困惑,当然我不是开发人员..

是否有其他方法可以做到这一点?我想当页面加载时我将有超过 10 000 个文件字符串继续进行。

编辑:

事实上我有一个 php 对象,它返回我这样的数组:

Array
(
[0] => Transmission\Model\File Object
    (
        [name:protected] => RootFolder/Folder1/File1.jpg
        [size:protected] => 13324
        [completed:protected] => 13324
        [client:protected] => 
    )

[1] => Transmission\Model\File Object
    (
        [name:protected] => RootFolder/Folder1/File2.mp3
        [size:protected] => 10383488
        [completed:protected] => 10383488
        [client:protected] => 
    )
[2] ...
)

我想做的是用当前文件的名称、大小和状态制作某种文件树表。我基于这个http://labs.abeautifulsite.net/archived/phpFileTree/demo/demo_classic.php

使用此对象,可以使用 $var->getName()、$var->getsize() 和 $var->isDone() 检索详细信息。

【问题讨论】:

    标签: php file tree directory treeview


    【解决方案1】:

    strtok会救你。

    <?php
    
    $input = [
      '/RootFolder/Folder1/File1.doc',
      '/RootFolder/Folder1/SubFolder1/File1.txt',
      '/RootFolder/Folder1/SubFolder1/File2.txt',
      '/RootFolder/Folder2/SubFolder1/File2.txt',
      '/RootFolder/Folder2/SubFolder1/SubSubFolder1/File4.doc',
    ];
    
    function parseInput($input) {
      $result = array();
    
      foreach ($input AS $path) {
        $prev = &$result;
    
        $s = strtok($path, '/');
    
        while (($next = strtok('/')) !== false) {
          if (!isset($prev[$s])) {
            $prev[$s] = array();
          }
    
          $prev = &$prev[$s];
          $s = $next;
        }
    
        $prev[] = $s;
    
        unset($prev);
      }
    
      return $result;
    }
    
    var_dump(parseInput($input));
    

    输出:

    array(1) {
      ["RootFolder"]=>
      array(2) {
        ["Folder1"]=>
        array(2) {
          [0]=>
          string(9) "File1.doc"
          ["SubFolder1"]=>
          array(2) {
            [0]=>
            string(9) "File1.txt"
            [1]=>
            string(9) "File2.txt"
          }
        }
        ["Folder2"]=>
        array(1) {
          ["SubFolder1"]=>
          array(2) {
            [0]=>
            string(9) "File2.txt"
            ["SubSubFolder1"]=>
            array(1) {
              [0]=>
              string(9) "File4.doc"
            }
          }
        }
      }
    }
    

    然后你就可以随心所欲地使用数组了。

    【讨论】:

    • 谢谢,我会试试这个!我让你知道它是否解决了我的问题。 :)
    • 太棒了!这正是我所期望的,我只需要制作一张桌子或类似的东西就可以得到我想要的。托马斯,你是英雄;)
    • 对不起,我忘了告诉我我是在一个对象上做这个。我在这里继续我的帖子:stackoverflow.com/questions/23895868/…
    【解决方案2】:

    您只需将根文件夹的路径传递给下一个函数即可:

    <?php
    $path = 'RootFolder';
    ListFolder($path);
    
        function ListFolder($path){
            //using the opendir function
            $dir_handle = @opendir($path) or die("Unable to open $path");
    
            //Leave only the lastest folder name
            $explode = explode("/", $path);
            $dirname = end($explode);
    
            //display the target folder.
            echo ("<li>$dirname\n");
            echo "<ul>\n";
            while (false !== ($file = readdir($dir_handle))) {
                if($file!="." && $file!=".."){
                    if (is_dir($path."/".$file)){
                        //Display a list of sub folders.
                        ListFolder($path."/".$file);
                    }else{
                        //Display a list of files.
                        echo "<li>$file</li>";
                    }
                }
            }
            echo "</ul>\n";
            echo "</li>\n";
    
            //closing the directory
            closedir($dir_handle);
        }
    ?>
    

    欲了解更多信息,请访问page

    【讨论】:

    • @Xatenev : 也许我的英语不是很好,但我知道他想建立文件夹和文件的树,我只通过根文件夹就很容易了,他不需要通过所有文件夹和文件路径
    • 我之前尝试过,但我的服务器负载很重。我不想列出我的根文件夹,项目太多了。这就像在 linux 服务器上列出“/”...
    猜你喜欢
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2011-10-29
    相关资源
    最近更新 更多