In what scenarios would using AJAX for loading folder structures be more beneficial than traditional PHP methods?

When dealing with large folder structures, using AJAX for loading them can provide a smoother user experience by allowing for asynchronous loading of content without having to reload the entire page. This can result in faster loading times and better performance, especially when dealing with nested folders or a large number of files.

// PHP code to retrieve folder structure
<?php

// Function to get folder structure
function getFolderStructure($path) {
    $files = scandir($path);
    
    $folders = array();
    $filesList = array();
    
    foreach($files as $file) {
        if($file == '.' || $file == '..') {
            continue;
        }
        
        if(is_dir($path . '/' . $file)) {
            $folders[] = $file;
        } else {
            $filesList[] = $file;
        }
    }
    
    return array('folders' => $folders, 'files' => $filesList);
}

// Get folder structure
$folderPath = 'path/to/your/folder';
$folderStructure = getFolderStructure($folderPath);

// Output JSON response
header('Content-Type: application/json');
echo json_encode($folderStructure);

?>