PHP按文件夹日期排序修改

时间:2017-05-25 06:44:11

标签: php sorting

我有一个多图库页面,显示按文件夹分隔的图像。问题是它在我的页面中按字母顺序显示(文件夹名称),如何将其更改为日期文件夹被修改为排序并显示在我的页面中?

<div class="pageWrap">          
    <div class="row">
        <div class="col-md-2 col-md-push">      
            <div class="panel panel-info">
                <div class="panel-heading"><center>Gallery</center></div>
                </div>
            </div>
        </div>
        <?php $files = scandir('galleries'); ?>
        <?php foreach ($files as $file): ?>

            <?php $dir = 'galleries/' . $file; ?>

            <?php if (is_dir($dir) && $file != '.' && $file != '..'): ?>
                <h2><?php echo ucwords($file); ?></h2>
                <?php $gallery = UGallery::init()->createGallery($dir, $file); ?>
            <?php endif; ?>
        <?php endforeach; ?>            
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

function build_sorter($dir) {
    return function ($f1, $f2) use ($dir) {
        return filemtime($dir.'/'.$f1) <= filemtime($dir.'/'.$f2) ? -1 : 1;
    }
}
$files = scandir('galleries');          
if(usort($files, build_sorter('galleries'))) {
    //...
}
相关问题