你应该在这里找到你的答案:
Sorting files by creation/modification date in PHP
还有其他类似的帖子,您可以在其中获得另一个有用的排序功能。
这样您的代码应如下所示:
if($h = opendir(dirname(realpath(__FILE__)).'/galerija/accomodation/')) {
$files = array();
while(($file = readdir($h) !== FALSE){
if($file !== '.' && $file !== '..'){
$files[] = stat($file);
}
}
// do the sort
usort($files, 'sortByName');
// do something with the files
foreach($files as $file) {
echo '<img src="galerija/accomodation/'.$file.'" rel="colorbox" />';
}
}
//some functions you can use to sort the files
//sort by change time
//you can change filectime with filemtime and have a similar effect
function sortByChangeTime($file1, $file2){
return (filectime($file1) < filectime($file2));
}
function sortByName{
return (strcmp($file1,$file2));
}