如何获取子文件夹的文件列表,并使用php将其写入JSON? [英] How to get a list of files of subfolders and write them in a JSON using php?

查看:45
本文介绍了如何获取子文件夹的文件列表,并使用php将其写入JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够列出所有文件名和当前目录/文件夹,但是我不知道如何为子目录创建JSON条目

I'm already able to list all the file names and current directory/folder but I don't know how to create JSON entries for subdirectories

这是我的代码

<?php
$dir = "office/";
if(is_dir($dir)){
    if($dh = opendir($dir)){
        while(($file = readdir($dh)) != false){
            if($file != "." and $file != ".."){
                $files_array[] = array('file' => $file); // Add the file to the array
            } 
        }
    }
    $return_array =array('dir' => $files_array);
    exit (json_encode($return_array));
}
?>

输出为

{
    "dir": [
        {
            "file": "FreeWallpapersApp.zip"
        },
        {
            "file": "20151211_ClipArtForTextView.7z"
        },
        {
            "file": "QRite.7z"
        },
        {
            "file": "CustomDialog_app_contacts.zip"
        },
        {
            "file": "LockScreenBasicApp.apk"
        },
        {
            "file": "ImgViewEffects.zip"
        },
      ]
    }

如何使用php在子文件夹中显示文件,并生成子目录中的文件名.

How to show files inside subfolder using php to also generate file names which are in subdirectories .

推荐答案

获得目录的递归列表的一种方法是使用递归函数.

One way to achieve the recursive listing of a directory is using a recursive function.

/*
 * Converts a filesystem tree to a PHP array.
 */
function dir_to_array($dir)
{
        if (! is_dir($dir)) {
                // If the user supplies a wrong path we inform him.
                return null;
        }

        // Our PHP representation of the filesystem
        // for the supplied directory and its descendant.
        $data = [];

        foreach (new DirectoryIterator($dir) as $f) {
                if ($f->isDot()) {
                        // Dot files like '.' and '..' must be skipped.
                        continue;
                }

                $path = $f->getPathname();
                $name = $f->getFilename();

                if ($f->isFile()) {
                        $data[] = [ 'file' => $name ];
                } else {
                        // Process the content of the directory.
                        $files = dir_to_array($path);

                        $data[] = [ 'dir'  => $files,
                                    'name' => $name ];
                        // A directory has a 'name' attribute
                        // to be able to retrieve its name.
                        // In case it is not needed, just delete it.
                }
        }

        // Sorts files and directories if they are not on your system.
        \usort($data, function($a, $b) {
                $aa = isset($a['file']) ? $a['file'] : $a['name'];
                $bb = isset($b['file']) ? $b['file'] : $b['name'];

                return \strcmp($aa, $bb);
        });

        return $data;
}

/*
 * Converts a filesystem tree to a JSON representation.
 */
function dir_to_json($dir)
{
        $data = dir_to_array($dir);
        $data = json_encode($data);

        return $data;
}

在您的示例中,

echo dir_to_json('office/');

将输出此

{ 
    "dir": [
        {
            "file": "FreeWallpapersApp.zip"
        },
        {
            "file": "20151211_ClipArtForTextView.7z"
        },
        // ...
        {
            "dir": [
                {
                    "file": "App.zip"
                },
                {
                    "file": "View.7z"
                },
                "name": "A Directory With Files",
            ]
        },
    ]
}

更新1:

我更新了答案,以对文件和目录进行明确排序.

Update 1:

I updated the answer to explicitly sort the files and directories.

这篇关于如何获取子文件夹的文件列表,并使用php将其写入JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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