Arrange files by creation or modification with PHP

2019-08-27 21:57发布

Is it possible to get the file names in a folder by the date of creation or modification?

Thanks.

标签: php file
3条回答
Evening l夕情丶
2楼-- · 2019-08-27 22:43

Yep, you need readdir() and filemtime().

查看更多
3楼-- · 2019-08-27 22:46

Get a list of the file names using normal methods (glob(), scandir(), whatever), and store the filenames in an array. Then loop through that array using filemtime() and store that value against your array. Finally, sort the array by your stored filemtime() value

Alternatively, look at DirectoryIterator and Sorting files by creation/modification date in PHP

查看更多
太酷不给撩
4楼-- · 2019-08-27 22:47
<?php
    $files = array();
    $it = new DirectoryIterator(".");
    $it->rewind();
    while ($it->valid()) { 
        $files[$it->getFilename()] = $it->getMTime(); 
        $it->next();
    }

    asort($files);
    $files = array_keys($files);
查看更多
登录 后发表回答