search

Home  >  Q&A  >  body text

PHP readdir sorting problem, how to sort by date

This is what it currently says:

function posts_get($directory,$ext)
{
    if (is_dir($directory)) {
    $handle = opendir($directory);
    while ($file = readdir($handle)){
    $subdir = $directory . '/' .$file;
    if ($file != '.' && $file !='..' && is_dir($subdir)){
    posts_get($subdir,$ext);
    } else if( $file != '.' && $file != '..') {
    $fileInfo = pathinfo($subdir);
    $fileExt = $fileInfo['extension'];
    if ($fileExt == $ext){
    echo 'File name:' . $file . '————' . 'Path:' . '//' .$_SERVER['HTTP_HOST'] . '/' .$directory . '/' . $file . ' <br />';
    }}}
    closedir($handle);
    }
}

Get the html files in the posts directory. There are 5 files in the test directory, 404.html, 405.html, 406.html, 407.html, 408.html

The output results are as follows:

File name:405.html————Path://hotbox.ryongyon.com/posts/405.html
File name: 406.html————Path://hotbox.ryongyon.com/posts/406.html
File name: 407.html————Path://hotbox.ryongyon.com/posts/407.html
File name: 408.html————Path://hotbox.ryongyon.com/posts/408.html
File name: 404.html————Path://hotbox.ryongyon.com/posts/404.html

How to sort readdir results according to the date of file creation

高洛峰高洛峰2789 days ago805

reply all(1)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-16 13:06:28

    • filectimeGet file creation time and convert to timestamp

    • Define an empty array and store data in the format 时间戳=>文件名

    • ksort或者krsortSort array

    • done

    reply
    0
  • Cancelreply