What is the best way to create a collapsible directory tree in PHP without using client-side scripting?

To create a collapsible directory tree in PHP without using client-side scripting, you can use PHP to generate HTML code with collapsible elements. This can be achieved by iterating through the directory structure using PHP functions like scandir() and recursion to build the tree structure. Each directory can be represented as a collapsible element with nested subdirectories displayed when expanded.

<?php
function generateDirectoryTree($path) {
    $html = '';
    $html .= '<ul>';
    $files = scandir($path);
    foreach ($files as $file) {
        if ($file != '.' && $file != '..') {
            $html .= '<li>' . $file;
            if (is_dir($path . '/' . $file)) {
                $html .= generateDirectoryTree($path . '/' . $file);
            }
            $html .= '</li>';
        }
    }
    $html .= '</ul>';
    return $html;
}

$directoryPath = '/path/to/directory';
echo generateDirectoryTree($directoryPath);
?>