sort files by date in PHP

后端 未结 5 1555
天涯浪人
天涯浪人 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:43

    I use your exact proposed code with only some few additional lines. The idea is more or less the same of the one proposed by @elias, but in this solution there cannot be conflicts on the keys since each file in the directory has a different filename and so adding it to the key solves the conflicts. The first part of the key is the datetime string formatted in a manner such that I can lexicographically compare two of them.

    if ($handle = opendir('.')) {
        $result = array();
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $lastModified = date('F d Y, H:i:s',filemtime($file));
                if(strlen($file)-strpos($file,".swf")== 4){
                    $result [date('Y-m-d H:i:s',filemtime($file)).$file] =
                        "$file$lastModified";
                }
            }
        }
        closedir($handle);
        krsort($result);
        echo implode('', $result);
    }
    

提交回复
热议问题