[toc]
# 准备好后端的现成套路
后端上,Codeigniter4已经提供了一个美好的函数来实现文件夹的递归读取,简单来说,就是文件夹里面的文件夹里面的文件夹里面的……都能读取。
得益于php7对GBK中文目录的支持,codeigniter的 `Filesystem Helper` ,就能得到全部目录(包含子目录)的数组:
[https://bcit-ci.github.io/CodeIgniter4/helpers/filesystem_helper.html](https://bcit-ci.github.io/CodeIgniter4/helpers/filesystem_helper.html)
Sub-folders contained within the directory will be mapped as well. If you wish to control the recursion depth, you can do so using the second parameter (integer). A depth of 1 will only map the top level directory:
第二个参数为递归深度,当深度设置为1的时候,只会显示该文件夹的根目录文件:
```php
$map = directory_map('./mydirectory/', 1);
```
By default, hidden files will not be included in the returned array. To override this behavior, you may set a third parameter to true (boolean):
默认情况下,隐藏文件不会显示到数组中。当需要的时候,将第三个参数设置为True。
```php
$map = directory_map('./mydirectory/', FALSE, TRUE);
```
Each folder name will be an array index, while its contained files will be numerically indexed. Here is an example of a typical array:
返回的数组样式:
```php
Array (
[libraries] => Array
(
[0] => benchmark.html
[1] => config.html
["database/"] => Array
(
[0] => query_builder.html
[1] => binds.html
[2] => configuration.html
[3] => connecting.html
[4] => examples.html
[5] => fields.html
[6] => index.html
[7] => queries.html
)
[2] => email.html
[3] => file_uploading.html
[4] => image_lib.html
[5] => input.html
[6] => language.html
[7] => loader.html
[8] => pagination.html
[9] => uri.html
)
```
# 准备好前端的现成套路
采用 [zui.sexy](http://zui.sexy/) 的 「树形菜单」现成方案。
[http://zui.sexy/#view/tree](http://zui.sexy/#view/tree)
![](https://box.kancloud.cn/3f3502ee47da8d80de697c98444345ed_710x435.png)
# 在Codeigniter中开工
首先,我在 `e:\xampp\htdocs\public\data\` 中新建了如下文件和文件夹,我日后的所有文件都会放在 `data` 这个文件夹中:
```
data
--20171015\
---- a.txt
---- b.txt
---- c.txt
--20171016\
---- a.txt
---- b.txt
---- c.txt
--20171017\
---- a.txt
---- b.txt
---- c.txt
--20171018\
---- a.txt
---- b.txt
---- c.txt
```
然后,我在 `Controller` 中新建一个 `Kanban.php`:
`e:\xampp\htdocs\application\Controllers\Kanban.php`
```
<?php namespace App\Controllers;
use CodeIgniter\Controller;
class Kanban extends Controller{
function __construct(){
helper('filesystem');
}
public function index(){
$map = directory_map('./');
d($map);
}
}//kanban
```
`directory_map('./')` 里面的 `./` 表示 `e:\xampp\htdocs\public\` ;
所以,当我要显示 `public\data` 文件夹内容的时候:
```
$map = directory_map('./data');
```
![](https://box.kancloud.cn/d302b9952d7139be10dd6f7a96683b61_641x310.png)
(完)