首页 > php文件操作 > php scandir遍历显示所有文件与文件夹下的文件

php scandir遍历显示所有文件与文件夹下的文件

scandir遍历显示所有文件与文件夹下的文件,方法很简单我们只要利用is_dir判断再递归查找一次,这样就可以把遍历目录下所有文件了,目录遍历代码如下:

<?php
function numfilesindir($thedir) {
    if (is_dir($thedir)) {
        $scanarray = scandir($thedir);
        for ($i = 0; $i < count($scanarray); $i++) {
            if ($scanarray[$i] != "." && $scanarray[$i] != "..") {
                if (is_file($thedir . "/" . $scanarray[$i])) {
                    echo $scanarray[$i] . "<br />";
                }
            }
        }
    } else {
        echo "Sorry, this directory does not exist.";
    }
}
echo numfilesindir("sample1");
?>

扫描指定位置的文件,代码如下:

<?php
print_r(scandir("/usr/local/apache2/htdocs"));
?>
<?php
$files = scandir(".", 1);
var_dump($files);
?>


文章网址:http://www.phprm.com/wenjian/fs4155.html

随意转载^^但请附上教程地址。

标签:php scandir 遍历文件

相关文章

发表留言