sort files by date in PHP

后端 未结 5 1633
天涯浪人
天涯浪人 2020-11-22 07:15

I currently have an index.php file which allows me to output the list of files inside the same directory, the output shows the names then I used filemtime() function to show

5条回答
  •  面向向阳花
    2020-11-22 07:54

    An example that uses RecursiveDirectoryIterator class, it's a convenient way to iterate recursively over filesystem.

    $output = array();
    foreach( new RecursiveIteratorIterator( 
        new RecursiveDirectoryIterator( 'path', FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS ) ) as $value ) {      
            if ( $value->isFile() ) {
                $output[] = array( $value->getMTime(), $value->getRealPath() );
            }
    }
    
    usort ( $output, function( $a, $b ) {
        return $a[0] > $b[0];
    });
    

提交回复
热议问题