How can AJAX Directory Listing be implemented in PHP to improve the user experience when browsing directories?

AJAX Directory Listing can be implemented in PHP to improve the user experience when browsing directories by dynamically loading directory contents without refreshing the page. This allows for a smoother and more interactive browsing experience for users.

<?php
if(isset($_POST['dir'])){
    $dir = $_POST['dir'];
    $files = scandir($dir);
    $files = array_diff($files, array('.', '..'));

    echo "<ul>";
    foreach($files as $file){
        echo "<li>$file</li>";
    }
    echo "</ul>";
}
?>