【问题标题】:Get the hierarchy of a directory with PHP使用 PHP 获取目录的层次结构
【发布时间】:2010-10-14 05:21:32
【问题描述】:

我正在尝试查找指定目录下的所有文件和文件夹

例如我有 /home/user/stuff

我想回来

/home/user/stuff/folder1/image1.jpg
/home/user/stuff/folder1/image2.jpg
/home/user/stuff/folder2/subfolder1/image1.jpg
/home/user/stuff/image1.jpg

希望这是有道理的!

【问题讨论】:

    标签: php file filesystems directory


    【解决方案1】:
    function dir_contents_recursive($dir) {
        // open handler for the directory
        $iter = new DirectoryIterator($dir);
    
        foreach( $iter as $item ) {
            // make sure you don't try to access the current dir or the parent
            if ($item != '.' && $item != '..') {
                if( $item->isDir() ) {
                    // call the function on the folder
                    dir_contents_recursive("$dir/$item");
                } else {
                    // print files
                    echo $dir . "/" .$item->getFilename() . "<br>";
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:
      foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f) {
          echo "$f \r\n";   
      }
      

      【讨论】:

        【解决方案3】:

        有效的解决方案(更改为您的文件夹名称)

        <?php
        $path = realpath('yourfolder/subfolder');
        ## or use like this
        ## $path = '/home/user/stuff/folder1';
        
        foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
        {
                echo "$filename\n";
        }
        ?>
        

        【讨论】:

          【解决方案4】:

          查找指定目录下的所有文件和文件夹。

          function getDirRecursive($dir, &$output = []) {
              $scandir = scandir($dir);
          
              foreach ($scandir as $a => $name) {
                  $path = realpath($dir . DIRECTORY_SEPARATOR . $name);
                  if (!is_dir($path)) {
                      $output[] = $path;
                  } else if ($name != "." && $name != "..") {
                      getDirRecursive($path, $output);
                      $output[] = $path;
                  }
              }
          
              return $output;
          }
          
          var_dump(getDirRecursive('/home/user/stuff'));
          

          输出(示例):

          array (size=4)
            0 => string '/home/user/stuff/folder1/image1.jpg' (length=35)
            1 => string '/home/user/stuff/folder1/image2.jpg' (length=35)
            2 => string '/home/user/stuff/folder2/subfolder1/image1.jpg' (length=46)
            3 => string '/home/user/stuff/image1.jpg' (length=27)
          

          【讨论】:

            【解决方案5】:
            $dir = "/home/user/stuff/";
            $scan = scandir($dir);
            
            foreach ($scan as $output) {
                echo "$output" . "<br />";
            }
            

            【讨论】:

              【解决方案6】:
              listAllFiles( '../cooktail/' ); //send directory path to get the all files and floder of root dir
              
              function listAllFiles( $strDir ) {
              
                  $dir = new DirectoryIterator( $strDir );
              
                  foreach( $dir as $fileinfo ) {
                      if( $fileinfo == '.' || $fileinfo == '..' ) continue;
              
                      if( $fileinfo->isDir() ) {
                          listAllFiles( "$strDir/$fileinfo" );
                      }
              
                      echo $fileinfo->getFilename() . "<br/>";
                  }
              }
              

              【讨论】:

                【解决方案7】:

                除了RecursiveDirectoryIterator解决方案之外还有glob()解决方案:

                // do some extra filtering here, if necessary
                function recurse( $item ) {
                    return is_dir( $item ) ? array_map( 'recurse', glob( "$item/*" ) ) : $item;
                };
                
                // array_walk_recursive: any key that holds an array will not be passed to the function. 
                array_walk_recursive( ( recurse( 'home/user/stuff' ) ), function( $item ) { print_r( $item ); } );
                

                【讨论】:

                  【解决方案8】:

                  您可以使用 RecursiveDirectoryIterator 甚至 glob 函数。 或者,scandir 函数将完成这项工作。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2021-01-20
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-04-05
                    • 1970-01-01
                    相关资源
                    最近更新 更多