What are some best practices for handling folder structures stored in a MySQL table for a download area in PHP applications?

When storing folder structures in a MySQL table for a download area in PHP applications, it is important to properly handle the hierarchy of folders and files. One way to achieve this is by using a recursive function to retrieve and display the folder structure. This function should query the database for folders and files, then recursively call itself to display nested folders and files.

<?php

// Function to display folder structure recursively
function displayFolderStructure($parent_id, $level = 0) {
    $folders = queryDatabaseForFolders($parent_id); // Function to query database for folders
    $files = queryDatabaseForFiles($parent_id); // Function to query database for files

    foreach ($folders as $folder) {
        echo str_repeat('-', $level) . $folder['name'] . "<br>";
        displayFolderStructure($folder['id'], $level + 1);
    }

    foreach ($files as $file) {
        echo str_repeat('-', $level) . $file['name'] . "<br>";
    }
}

// Call the function to display the folder structure starting from the root folder
displayFolderStructure(0);

?>