[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)

# 在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');
```

(完)