4

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

Here is my code

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

and output is

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

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

Amaury Medeiros
  • 2,093
  • 4
  • 26
  • 42
Attaullah
  • 3,856
  • 3
  • 48
  • 63
  • Here is how you can get files recursive, you just need to json encode => http://stackoverflow.com/questions/14304935/php-listing-all-directories-and-sub-directories-recursively-in-drop-down-menu – ka_lin Dec 25 '15 at 21:01
  • sorry here is something else :( – Attaullah Dec 25 '15 at 21:02
  • http://php.net/manual/en/class.directoryiterator.php here you go, the reason why you are not getting sub-directories, is because you are pushing all directories into same array for sub directories you would need multi-dimensional array – Siim Kallari Dec 25 '15 at 21:26

1 Answers1

8

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

In your example,

echo dir_to_json('office/');

would output this

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

Update 1:

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

Daniele Orlando
  • 2,692
  • 3
  • 23
  • 26