What are some best practices for creating a download area with subfolders using PHP and JavaScript?

When creating a download area with subfolders using PHP and JavaScript, it is important to organize the files into subfolders for better organization and user experience. One way to achieve this is by creating a PHP script that scans the directory structure and generates the necessary HTML elements to display the files and folders. Additionally, JavaScript can be used to handle user interactions such as folder expansion and file downloads.

<?php
$dir = 'downloads/';

function listFiles($dir){
    $files = array_diff(scandir($dir), array('..', '.'));
    
    echo '<ul>';
    foreach($files as $file){
        if(is_dir($dir . $file)){
            echo '<li><strong>' . $file . '</strong>';
            listFiles($dir . $file . '/');
            echo '</li>';
        } else {
            echo '<li><a href="' . $dir . $file . '">' . $file . '</a></li>';
        }
    }
    echo '</ul>';
}

listFiles($dir);
?>