after using $files = new DirectoryIterator() in PHP, how do you sort the items?

后端 未结 2 1364
半阙折子戏
半阙折子戏 2020-12-11 19:35

We can get the files in a directory in PHP by

$files = new DirectoryIterator() 

after that is there an easy way to sort the items in a part

2条回答
  •  一整个雨季
    2020-12-11 20:04

    $files = new DirectoryIterator($path);
    $i = 0;
    $paths = array();
    while($files->valid()) {
        $paths[$i++] = $files->getFileName();
        $files->next();
    }
    sort($paths)
    

    May be what you are looking for, you can always of course apply the sort function to sort the paths depending on your preference after that.

提交回复
热议问题