You might start with something like the following script.
I've added comments to explain it.
For sorting things, change the while-loop to not output directly, but to store dir or file in an array by type. Then return that array, let's name it dircontent.
Then apply your sort() function to dircontent.
Then foreach over dircontent to output the sorted files and folders (you saved the type before, so you know again, if its file or folder).
If you want this to go deeper in the hierarchie, put a showDir($dir) inside the is_dir() check.
<?php
/**
* List the folders of a dir and show only PHP files.
*/
function showDir($dir)
{
$handle = opendir($dir);
while ($dir = readdir($handle)) {
// exclude dot files/folders
if ($dir === '.' or $dir === '..') {
continue;
}
// is this a dir?
if(is_dir($dir)) {
echo '<a href=' . $dir . '>' . $dir . '<a><br>';
}
// is it a file?
if(is_file($dir)) {
// get file extension, in order to check if it's a PHP file
$ext = pathinfo($dir, PATHINFO_EXTENSION);
// is it a PHP file?
if($ext === 'php') {
// indent files a bit
echo '|- ' . $dir . '<br>';
}
}
}
closedir($handle);
}
showDir(".");
?>
Keep in mind, that there are other solutions around: to name a few scandir(), glob() or DirectoryIterator(). The opendir()/readdir()/closedir() approach is a bit rusty, but works.