Home  >  Article  >  php教程  >  遍历某文件夹下的所有文件和文件夹方法一

遍历某文件夹下的所有文件和文件夹方法一

PHP中文网
PHP中文网Original
2016-05-25 17:15:48953browse

php代码

<?php
/**
 * 获取当前目录及子目录下的所有文件
 * @param string $dir 路径名
 * @return array 所有文件的路径数组
 */
//常规方法
function get_files($dir) {
    $files = array();
 
    if(!is_dir($dir)) {
        return $files;
    }
 
    $handle = opendir($dir);
    if($handle) {
        while(false !== ($file = readdir($handle))) {
            if ($file != &#39;.&#39; && $file != &#39;..&#39;) {
                $filename = $dir . "/"  . $file;
                if(is_file($filename)) {
                    $files[] = $filename;
                }else {
                    $files = array_merge($files, get_files($filename));
                }
            }
        }   //  end while
        closedir($handle);
    }
    return $files;
}   //  end function

print_r(get_files($dir));
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn