简体   繁体   中英

php directory listing files in numerical order

How would I change this function so that it listed the files in order as currently they are sorted randomly. I have already changed it so it excludes the Index.php file. Any help would be much appreciated.

function DirDisply() {
$TrackDir=opendir(".");

while ($file = readdir($TrackDir)) {
$info = pathinfo($file);
$file_name =  basename($file,'.'.$info['extension']);
if ($file == "." || $file == ".." || $file == "index.php"){ }
else {
print "<p><a class=btn href=$file target=content>Item Number $file_name</a></p>";

}
}
closedir($TrackDir);
return;
}

考虑使用glob()并对数组进行所需的排序。

If you wanted to continue to use readdir() you'd have to set a new array inside your while loop, and then manage the array sorting it in whatever order you want.

Personally I'd switch to scandir() as it has a sorting option built in and already sorts alphabetically in ascending order into an array (less code, easier to maintain etc):
http://www.php.net/manual/en/function.scandir.php

Either way you would have to have the foreach on the array, so scandir() is the better approach as it means no while loop as well.

scandir() returns an array, which can (arguably) be easy to manage than looping through the folder with readdir() as you can check a final outcome in one go. IE check the array is an array, has keys, values, loop it, check each one on loop, etc.

Then rather than while as you currently have, just use scandir() and foreach the array which scandir() returns.

Example as requested (although it's just from php.net, it is a good basic example):

$dir    = 'yourdir';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);  //Reverse order

print_r($files1);
print_r($files2);

The 1 in $files2 reverses the order. The default sorting order is ascending (0 but no need to specify this) and 1 for descending.
Of course you'd loop the array ( $files1 ) using your code:
print "<p><a class=btn href=$file target=content>Item Number $file_name</a></p>";
Where the value of the array you loop will be your filename.

You should have a read at the php net for all these functions whenever you use one. If you looked up your original function readdir() on php.net there is (nearly) always links to similar functions, and you would have found scandir() ;)

SIDE NOTE:
Also. you check if something = something and if true do nothing, else do something. This is not the right approach for this scenario.
You should just do something if it = something, such as:

if ($file != "." || $file != ".." 
    || $file != "index.php")
  { 
    print "<p><a class=btn 
           href=$file target=content>Item Number $file_name</a></p>";
  }

You could even have an else report an error or "no files" or whatever.

If you want some natural ordering you can combine scandir and natsort.

    $images = array_diff(scandir($the_path), array('..', '.'));
    natsort($images);
    foreach ($images as $img) {
        // TODO
    }

natsort natural ordering from php.net :

This function implements a sort algorithm that orders alphanumeric strings in the way a human being would

It's an alphabetical order but it takes care of index number in similar filenames :

file-2.jpg
file-1.jpg
file-10.jpg 

will be ordered

file-1.jpg
file-2.jpg
file-10.jpg 

scandir, on the other hand, will only order in alphabetical order like :

file-1.jpg
file-10.jpg
file-2.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM