Press "Enter" to skip to content

php遍历一个文件夹下的所有文件和子文件夹

Last updated on 2019-05-17

<?php
//写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。
function my_scandir($dir)
{
    $files = array();
    if (is_dir($dir)) {
        if ($handle = opendir($dir)) {
            while (($file = readdir($handle)) !== false) {
                if ($file != "." && $file != "..") {
                    if (is_dir($dir . "/" . $file)) {
                        $files[$file] = my_scandir($dir . "/" . $file);
                    } else {
                        $files[] = $dir . "/" . $file;
                    }
                }
            }
            closedir($handle);
            return $files;
        }
    }
}

echo "<pre>";
print_r(my_scandir("img"));
echo "</pre>";

 

Leave a Reply

Your email address will not be published. Required fields are marked *