How to get X newest files from a directory in PHP?

前端 未结 3 1953
南方客
南方客 2020-12-19 08:12

The code below is part of a function for grabbing 5 image files from a given directory.

At the moment readdir returns the images \'in the order in which they are sto

3条回答
  •  情话喂你
    2020-12-19 08:42

    Or you can create function for the latest 5 files in specified folder.

    private function getlatestfivefiles() {
        $files = array();
        foreach (glob("application/reports/*.*", GLOB_BRACE) as $filename) {
            $files[$filename] = filemtime($filename);
        }
        arsort($files);
    
        $newest = array_slice($files, 0, 5);
        return $newest;  
    }
    

    btw im using CI framework. cheers!

提交回复
热议问题