Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php遍历目录下所有文件和子文件夹的代码

php遍历目录下所有文件和子文件夹的代码

发布时间:2016-07-18   编辑:www.jquerycn.cn
介绍一个可以遍历目录下所有文件与子文件夹的代码,供初学的朋友参考。

例子:

<?php
/**
* 遍历目录下所有文件及子文件夹
* edit www.jbxue.com
*/
function read_all_dir ( $dir )
{
$result = array();
$handle = opendir($dir);
if ( $handle )
{
while ( ( $file = readdir ( $handle ) ) !== false )
{
if ( $file != '.' && $file != '..')
{
$cur_path = $dir . DIRECTORY_SEPARATOR . $file;
if ( is_dir ( $cur_path ) )
{
$result['dir'][$cur_path] = read_all_dir ( $cur_path );
}
else
{
$result['file'][] = $cur_path;
}
}
}
closedir($handle);
}
return $result;
}
?>

代码不复杂,有兴趣的朋友,自己找几个目录测试下。

您可能感兴趣的文章:
php无限遍历目录代码
php遍历目录下所有文件和子文件夹的代码
PHP遍历文件和文件夹的小例子
php目录遍历与删除的代码一例
删除指定文件夹中所有文件的php代码
PHP遍历目录下所有文件的小例子
php遍历目录与其下所有文件
php 读取文件夹与文件夹中文件的函数
php 目录遍历与删除的函数示例
php 读取目录文件夹列表的例子

[关闭]