简体   繁体   English

在音乐文件目录php中显示最新文件

[英]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. 因此,II现在运行代码,它应该仅显示一个目录,并且仅显示最新目录。

$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. 1按glob获取目录列表。
2 Sort lists by filectime. 2按filectime对列表进行排序。
3 Get a result if it exists. 3获取结果(如果存在)。

$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. 它只会给您一个,也只有最新的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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