使用 PHP 获取目录的层次结构 [英] Get the hierarchy of a directory with PHP

查看:24
本文介绍了使用 PHP 获取目录的层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如我有/home/user/stuff

For example I have /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

希望这是有道理的!

推荐答案

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

这篇关于使用 PHP 获取目录的层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆