duanliao5995 2013-08-15 11:10
浏览 25
已采纳

文件结构为多维数组

I need to store all directories and files in multidimensional array for a given location (sample : /path/to/folder) . It should include the sub folders as well no matter how deep.

Edited :

I tried with php scandir and it gave me following results,

code :

$dir    = '/path/to/folder';
$files = scandir($dir);
print_r($files);

results :

Array
(
    [0] => .
    [1] => ..
    [2] => folder1
    [3] => file1
    .....
)

What I need is to get the content inside the folder1 and do it continuously till I get complete folder structure with all files.

P.S : I need to remove "." and ".." from the result array too.

  • 写回答

1条回答 默认 最新

  • douweng3383 2013-08-15 11:10
    关注

    for file listing php provide readdir and scandir functions.

    I preferred sccandir since it's new(php 5>) and we can use following recursive function for this.

    Need to provide the directory path (/path/to/your/folder) as a parameter and it will return multidimensional array with folder structure.

    function dirToArray($dir) {
        $result = array();
        $cdir = scandir($dir);
        foreach ($cdir as $key => $value) {
            if (!in_array($value,array(".","..")))  {
                if (is_dir($dir . DIRECTORY_SEPARATOR . $value)){
                    $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
                } else {
                    $result[] = $value;
                }
            }
        }
        return $result;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题