Can anyone tell me how to show the contents of a directory in alphabetical order?
Currently I am using the following script and it shows the files based on the date they were created.
<?php
// directory path can be either absolute or relative
$dirPath = 'docs/';
// open the specified directory and check if it's opened successfully
if ($handle = opendir($dirPath)) {
// keep reading the directory entries 'til the end
while (false !== ($file = readdir($handle))) {
// just skip the reference to current and parent directory
if ($file != "." && $file != "..") {
if (is_dir("$dirPath/$file")) {
// found a directory, do something with it?
echo "[$file]<br>";
} else {
// found an ordinary file
echo "<fieldset>
<label><a href='docs/$file'>$file</label>
</fieldset>";
}
}
}
// ALWAYS remember to close what you opened
closedir($handle);
}
?>
I’ve had a look around and found something about scan dir instead of read dir but I can’t get it to work at all.
Sounds like that would work, thanks for the super fast reply. Can you tell me how I would achieve this please? Sorry I’m not very good with arrays in php.
Then you’re out of luck. You could try removing the public/protected/private keywords, but I think the SPL requirements would still hinder you.
Best bet then, add the filenames to an array instead of echo’ing them out. Once you’ve collected all of them, sort and then display. As per Guido2004’s original suggestion.