Basically I have a php script that lists all the files in a directory and creates links to them. However it seems to order the files in a very arbitrary fashion (not according alphabetically/by date/ by size etc…). What way does php order the files? Is there any way to change this?
<?php
$orig = "/var/www/phplist";
if ($dir = opendir($orig)) {
echo "from: $orig";
echo "<br><br>";
while ($file = readdir($dir)) {
$ok = "true";
$filename = $file;
if ($file == "."){
$ok = "false";
}
else if ($file == ".."){
$ok = "false";
}
if ($ok == "true"){
echo "<a href= '/phplist/$file'>$filename</a>";
echo "<br>";
}
}
closedir($dir);
}
?>
The documentation for this function explains what you’re looking for:
And depending on the filesystem, the files might retain the order in which they were created (FAT); might be sorted alphabetically (NTFS); might be sorted according to their position in a hash table (ext4); or various other ways.
If you need files in a specific order look at scandir which takes a parameter that specifies the sort order.