在主文件夹和子文件夹中搜索.mp3文件

Search for .mp3 files in main folder and subfolders

本文关键字:文件夹 搜索 mp3 文件 主文件      更新时间:2023-09-26

我希望从头开始制作一个网络播放器,这样我就可以从手机上控制我的音乐。我已经完成了文件夹中所有文件的列表,但我需要让它列出主文件夹子文件夹中的文件。

所以有点像 ''Path'' - 在 ''Path'' 中显示所有内容,

而 ''Path''subs'' 显示所有子文件夹的内容。

这是我编写的代码,允许我在文件夹中搜索.mp3文件,但我也无法固定在子文件夹中搜索的方式。

$.ajax({
    url: "music/",
    success: function(data){
        $(data).find("a:contains(.mp3)").each(function(){
            var files = $(this).text().replace(/  /g, '');
            files = $(this).text().replace(/.mp3/,'');
            $('<p></p>').html(files).appendTo('#displayer');
            $('#displayer > p:nth-child(odd)').css('background-color','#DDD');
        });
    }
});

提前非常感谢

这是我

PHP中的解决方案

<?php
// your mp3 dir
$dir = "dir/";
$result = array();
if(is_dir($dir)){
    $iterator = new RecursiveDirectoryIterator($dir);
    foreach(new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file){
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        if("mp3"==$ext){
            $result[] = $file->getPathName();
        }
    }
    // you can echo json_encode(yourMp3Path) here.
    var_dump($result);
}