duansengcha9114 2017-04-17 06:18
浏览 9
已采纳

在PHP中列出来自目录和子目录的数组中的MP3文件

I Want To Get List Of Files In My Directories & Sub-Directories In An Array In PHP Language .

I Have 2 Type Of Code :

1- First Code:
This Bellow Code List All Files In An Array , But There Are Folders And Sub-directories In Array :

$files = dir_scan('pathAddress'); 
function dir_scan($folder) {
$files = glob($folder);
foreach ($files as $f) {
    if (is_dir($f)) {
        $files = array_merge($files, dir_scan($f .'/*')); // scan subfolder
    }
}
return $files;
}
echo "<pre>";
print_r($files);
echo "</pre>";

Result Of Top Code : Click For View Image

2- Second Code:
This Bellow Code List All MP3 Files But In String Not Array! & I Can't Convert It To Array.

$scan_it = new RecursiveDirectoryIterator("pathAddress");
foreach(new RecursiveIteratorIterator($scan_it) as $file) {
if (strtolower(substr($file, -4)) == ".mp3") {
echo "<pre>";
echo($file);
echo "</pre>";
}
}

Result Of Top Code : Click For View Image

Finally, I Want An Array Of MP3 Files In All Directories & Sub-Directories Specified Location .

Thanks For Your Help

  • 写回答

1条回答 默认 最新

  • douzhang2092 2017-04-17 06:56
    关注

    This code might help you, It will check all the folders and in return, will get file names ..

    <?php 
    
    function listFolderFiles($dir)
    {    
        $file_names = array();
        foreach (new DirectoryIterator($dir) as $fileInfo) {
            if (!$fileInfo->isDot()) {
    
    
    
                if ($fileInfo->isDir()) {
    
    
                    // checking directory empty or not, if not then append list     
                    $isDirEmpty = !(new \FilesystemIterator($fileInfo->getPathname()))->valid();
    
                    if($isDirEmpty != 1)
                    {
    
                        $file_names[] = listFolderFiles($fileInfo->getPathname());
                    }
    
    
    
                }
                else 
                {
                    $file_names[] = $fileInfo->getPathname() ;  
                }
            }
        }
    
    
        // Splicing Array 
            for ($i=0; $i<count($file_names); $i++) {
    
                if (is_array($file_names[$i])) {
                        array_splice($file_names, $i, 1, $file_names[$i]);
                }
            }
    
    
        return $file_names;
    }
    
    
    $res = listFolderFiles('main_folder_name');
    echo '<pre>';
    print_r($res);
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了