<?php
/**
* 将读取到的目录以数组的形式展现出来
* @return array
* opendir() 函数打开一个目录句柄,可由 closedir(),readdir() 和 rewinddir() 使用。
* is_dir() 函数检查指定的文件是否是目录。
* readdir() 函数返回由 opendir() 打开的目录句柄中的条目。
* @param array $files 所有的文件条目的存放数组
* @param string $file 返回的文件条目
* @param string $dir 文件的路径
* @param resource $handle 打开的文件目录句柄
*/
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("E:\wamp64\www\php_test")); //电脑的文件路径即可
结果如下:
Array
(
[.idea] => Array
(
[0] => E:\wamp64\www\php_test/.idea/encodings.xml
[1] => E:\wamp64\www\php_test/.idea/misc.xml
[2] => E:\wamp64\www\php_test/.idea/modules.xml
[3] => E:\wamp64\www\php_test/.idea/php_test.iml
[4] => E:\wamp64\www\php_test/.idea/workspace.xml
)
[0] => E:\wamp64\www\php_test/curl.php
[1] => E:\wamp64\www\php_test/dir.php
[2] => E:\wamp64\www\php_test/dqdz.php
[3] => E:\wamp64\www\php_test/foreach.php
[4] => E:\wamp64\www\php_test/moshu.php
[5] => E:\wamp64\www\php_test/MysqlModel.class.php
[6] => E:\wamp64\www\php_test/mysql_connect.php
[7] => E:\wamp64\www\php_test/mysql_test.php
[page_ajax] => Array
(
[0] => E:\wamp64\www\php_test/page_ajax/article.php
[1] => E:\wamp64\www\php_test/page_ajax/article.sql
[2] => E:\wamp64\www\php_test/page_ajax/index.php
[3] => E:\wamp64\www\php_test/page_ajax/jquery.min.js
[4] => E:\wamp64\www\php_test/page_ajax/jquery.paginate.js
[5] => E:\wamp64\www\php_test/page_ajax/loading.gif
[6] => E:\wamp64\www\php_test/page_ajax/style.css
)
[8] => E:\wamp64\www\php_test/page_ajax.zip
[9] => E:\wamp64\www\php_test/paixu.php
[10] => E:\wamp64\www\php_test/test.php
)