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

发布于 2024-07-15 20:00:19 字数 262 浏览 3 评论 0原文

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

例如我有 /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

希望这是有道理的!

I'm trying to find all the files and folders under a specified directory

For example I have /home/user/stuff

I want to return

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

Hopefully that makes sense!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

温柔女人霸气范 2024-07-22 20:00:19
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>";
            }
        }
    }
}
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>";
            }
        }
    }
}
我是有多爱你 2024-07-22 20:00:19
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f) {
    echo "$f \r\n";   
}
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f) {
    echo "$f \r\n";   
}
﹏雨一样淡蓝的深情 2024-07-22 20:00:19

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

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

The working solution (change with your folder name)

<?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";
}
?>
森林迷了鹿 2024-07-22 20:00:19
$dir = "/home/user/stuff/";
$scan = scandir($dir);

foreach ($scan as $output) {
    echo "$output" . "<br />";
}
$dir = "/home/user/stuff/";
$scan = scandir($dir);

foreach ($scan as $output) {
    echo "$output" . "<br />";
}
得不到的就毁灭 2024-07-22 20:00:19

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

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)

Find all the files and folders under a specified directory.

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'));

Output (example) :

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)
离旧人 2024-07-22 20:00:19
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/>";
    }
}
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/>";
    }
}
青柠芒果 2024-07-22 20:00:19

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

Beside RecursiveDirectoryIterator solution there is also glob() solution:

// 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 ); } );
心的憧憬 2024-07-22 20:00:19

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

You can use the RecursiveDirectoryIterator or even the glob function.
Alternatively, the scandir function will do the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文