从目录和列表中列出阵列中的MP3文件PHP

时间:2017-04-17 06:18:52

标签: php arrays

我想获取我的目录中的文件列表& PHP语言中数组的子目录。

我有2种代码:

1-首要代码:
此Bellow Code列出数组中的所有文件,但数组中有文件夹和子目录:

$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>";

热门代码的结果: Click For View Image

2-秒代码:
此Bellow Code列出所有MP3文件,但字符串不是数组! &安培;我无法将其转换为数组。

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

热门代码的结果: Click For View Image

最后,我想在所有目录中放置一系列MP3文件&amp;子目录指定的位置。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

此代码可能对您有所帮助,它将检查所有文件夹,作为回报,将获取文件名..

<?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);


?>