What alternative function can be used instead of readdir() for sorting files in PHP?

When sorting files in PHP, an alternative function that can be used instead of readdir() is scandir(). scandir() reads the contents of a directory and returns an array of sorted file and directory names. This function eliminates the need for manually sorting the files returned by readdir().

$directory = "/path/to/directory";

// Get the list of files in the directory sorted alphabetically
$files = scandir($directory);

// Loop through the sorted files
foreach($files as $file){
    echo $file . "<br>";
}