sort files by date in PHP

后端 未结 5 1553
天涯浪人
天涯浪人 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] =
                        "<tr><td><input type=\"checkbox\" name=\"box[]\"></td><td><a href=\"$file\" target=\"_blank\">$file</a></td><td>$lastModified</td></tr>";
                }
            }
        }
        closedir($handle);
        krsort($result);
        echo implode('', $result);
    }
    
    0 讨论(0)
  • 2020-11-22 07:50

    This would get all files in path/to/files with an .swf extension into an array and then sort that array by the file's mtime

    $files = glob('path/to/files/*.swf');
    usort($files, function($a, $b) {
        return filemtime($a) < filemtime($b);
    });
    

    The above uses an Lambda function and requires PHP 5.3. Prior to 5.3, you would do

    usort($files, create_function('$a,$b', 'return filemtime($a)<filemtime($b);'));
    

    If you don't want to use an anonymous function, you can just as well define the callback as a regular function and pass the function name to usort instead.

    With the resulting array, you would then iterate over the files like this:

    foreach($files as $file){
        printf('<tr><td><input type="checkbox" name="box[]"></td>
                <td><a href="%1$s" target="_blank">%1$s</a></td>
                <td>%2$s</td></tr>', 
                $file, // or basename($file) for just the filename w\out path
                date('F d Y, H:i:s', filemtime($file)));
    }
    

    Note that because you already called filemtime when sorting the files, there is no additional cost when calling it again in the foreach loop due to the stat cache.

    0 讨论(0)
  • 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];
    });
    
    0 讨论(0)
  • 2020-11-22 07:58

    You need to put the files into an array in order to sort and find the last modified file.

    $files = array();
    if ($handle = opendir('.')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
               $files[filemtime($file)] = $file;
            }
        }
        closedir($handle);
    
        // sort
        ksort($files);
        // find the last modification
        $reallyLastModified = end($files);
    
        foreach($files as $file) {
            $lastModified = date('F d Y, H:i:s',filemtime($file));
            if(strlen($file)-strpos($file,".swf")== 4){
               if ($file == $reallyLastModified) {
                 // do stuff for the real last modified file
               }
               echo "<tr><td><input type=\"checkbox\" name=\"box[]\"></td><td><a href=\"$file\" target=\"_blank\">$file</a></td><td>$lastModified</td></tr>";
            }
        }
    }
    

    Not tested, but that's how to do it.

    0 讨论(0)
  • 2020-11-22 08:00
    $files = array_diff(scandir($dir,SCANDIR_SORT_DESCENDING), array('..', '.'));
    print_r($files);
    
    0 讨论(0)
提交回复
热议问题