Sorting with opendir

can anyone tell me how to sort a list of file names and direcories with opendir alpha numerically? my script is attached. thanks!

You first pull in the list of files, save it to an array, then sort that array before displaying.

Why does this not work? I added the code in the red.

/* open the directory */
$dirFiles = array();
$CURRENT_DIR = “$exec_root_dir”;
if ($MY_DIR) { $CURRENT_DIR = “$CURRENT_DIR/$MY_DIR”; }
$handle = @opendir($CURRENT_DIR) or die(“Unable to open this directory”);

/* loop through files in directory */
while (false!==($file = readdir($handle))) {

$dirFiles[] = $file;

    /* watch out for . and .. */
    if ($file != "." && $file != "..")

{

sort($dirFiles);
foreach($dirFiles as $file)

            /* process as directory only if one level deep*/
            if (is_dir("$CURRENT_DIR/$file") && !$MY_DIR) {
                    print("
                            <form action='index.php' method='post'>
                                    <input type='hidden' name='MY_DIR' size='20' value='$file'>
                                    <input type='submit' value='$file' name='D-type' class='btn3' onmouseover='this.className=\\"btn3 btnhov\\"' onmouseout='this.className=\\"btn3\\"' />
                            </form>
                    ");
            }

            /* process as file */
            if (is_file("$CURRENT_DIR/$file") && is_executable("$CURRENT_DIR/$file") ) {
                    $fp = fopen("$CURRENT_DIR/$file",'r');
                    $filetext = fread($fp,filesize("$CURRENT_DIR/$file"));
                    fclose($fp);

                    /* do we want to display this file? */
                    if ( preg_match('/#\\$exec_allow=yes/',$filetext)  || $exec_allow_all) {
                            /* pull out output file */
                            if ( preg_match('/#\\$exec_out=(.*)/',$filetext,$matches) ) { $exec_out=$matches[1]; }

                            /* pull out description or use filename */
                            if ( preg_match('/#\\$exec_name=(.*)/',$filetext,$matches) ) { $exec_name=$matches[1]; }
                            else { $exec_name=$file; }

// print (“$exec_out<br>”);
print("
<form action=‘index.php’ method=‘post’>
<input type=‘hidden’ name=‘MY_DIR’ size=‘900’ value=‘$MY_DIR’>
<input type=‘hidden’ name=‘MY_FILE’ size=‘900’ value=‘$file’>
<input type=‘hidden’ name=‘MY_DESC’ size=‘900’ value=‘$exec_name’>
<input type=‘hidden’ name=‘MY_OUT’ size=‘900’ value=‘$exec_out’>
<input type=‘hidden’ name=‘MY_VIEW’ size=‘900’ value=‘0’>
<input type=‘submit’ value=‘$exec_name’ name=‘F-type’ class=‘btn2’ onmouseover=‘this.className=\“btn2 btnhov\”’ onmouseout=‘this.className=\“btn2\”’ />
</form>
");
}
}
}
}

/* close the directory */
closedir($handle);