简体   繁体   中英

Display the newest file in directory of music files, php

I have a directory with many subdirectories. These sub directories act as "playlists" (they contain many music files). The code below will show all of the subdirectories (or playlists). I want to display Only ONE, and I want it to be the newest subdirectory created. So II run the code now it should display only one and only the newest directory.

$y=0;

        foreach(glob('../music/*', GLOB_ONLYDIR) as $playlist) {
            $y=$y+1;

            echo "\n";
            echo '<div class="mp_content" id="c_album_'.$y.'">';
            echo "\n";
            echo '<img src="music/album1/album.jpg" alt="album1"/>';
            echo "\n";
          }

1 Get directory lists by glob.
2 Sort lists by filectime.
3 Get a result if it exists.

$lists = glob('../work/*', GLOB_ONLYDIR);
usort($lists, function($a, $b){return filectime($a) <= filectime($b);});
if(isset($lists[0])) {
    print($lists[0]);
}

It will give you only one and only the newest directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM