使用PHP获取目录的层次结构

8
我正在尝试查找指定目录下的所有文件和文件夹。例如,我有/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

希望你能理解!
8个回答

11
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>";
            }
        }
    }
}

5
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f) {
    echo "$f \r\n";   
}

2

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

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

1
在指定目录下找到所有的文件和文件夹。
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)

1
$dir = "/home/user/stuff/";
$scan = scandir($dir);

foreach ($scan as $output) {
    echo "$output" . "<br />";
}

0
除了使用 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 ); } );

0
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/>";
    }
}

0

您可以使用RecursiveDirectoryIterator或者glob函数。另外,scandir函数也可以完成这项工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接