代码拉取完成,页面将自动刷新
同步操作将从 凌达/PHP-Markdown 接口文档管理工具 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<?php
//后根序遍历目录, 读取所有子目录和文件
//张志斌 954861399@qq.com
class Dir
{
public $dir = '';
public $fileList = array();
public function __construct($dir)
{
$stack = array();
$this->dir = $dir = realpath($dir);
$currentDir = $dir.DIRECTORY_SEPARATOR;
$current = array_slice(scandir($dir), 2); //去掉 . 和 ..
if (!empty($current)) {
do {
$isOver = 1;
foreach ($current as $k => $fileName) {
if (substr($fileName, 0, 1) != '.') {
$pathName = $currentDir.$fileName;
if (is_file($pathName)) {
$this->fileList[] = $pathName;
} elseif (is_dir($pathName) && count(scandir($pathName)) > 2) {
//当前是目录, 就把其以后的数据压栈
$tmp = array_slice($current, $k+1);
$tmp['currentDir'] = $currentDir;
array_push($stack, $tmp);
$current = array_slice(scandir($pathName), 2);
$currentDir = $pathName.DIRECTORY_SEPARATOR;
$isOver = 0;
break;
}
}
}
if ($isOver) { //没有入栈, 说明当前目录里全是文件
$current = array_pop($stack);
$currentDir = $current['currentDir'];
unset($current['currentDir']);
}
} while(!empty($stack) || count($current));
}
}
//读取所有子目录和文件, $dir目录最后不要有'/'
public static function ini($dir)
{
return new self($dir);
}
//替换路径里的字符串
public function replace($target, $replace='')
{
foreach ($this->fileList as $k => $v) {
$this->fileList[$k] = str_replace($target, $replace, $v);
}
return $this;
}
//筛选获取指定的后缀
public function extension($ext)
{
foreach ($this->fileList as $k => $v) {
$extension = pathinfo($v, PATHINFO_EXTENSION);
if (strtolower($extension) != $ext) {
unset($this->fileList[$k]);
}
}
return $this;
}
//重新整理文件列表, 给每一个目录和文件标上唯一的整数id
public function attachUniqueId()
{
$list[] = array('currentId' => 1, 'preId' => -1, 'name' => 'Root');
$preId = 1; //父节点索引
$i = 2; //当前索引
$nodeIndex = array(); //已生成索引的节点
foreach ($this->fileList as $k => $srcPathName) {
$path = str_replace($this->dir, '', $srcPathName);
$arrNode = explode(DIRECTORY_SEPARATOR, ltrim($path, DIRECTORY_SEPARATOR));
if (count($arrNode) == 1) {
$list[] = array('currentId' => $i++, 'preId' => 1, 'name' => reset($arrNode), 'isEnd' => 1);
} else {
//第一个节点(第一层目录), 父节点固定为1
$firstNode = array_shift($arrNode);
if (empty($nodeIndex[$firstNode])) {
$nodeIndex[$firstNode] = $i++;
$list[] = array('currentId' => $nodeIndex[$firstNode], 'preId' => 1, 'name' => $firstNode, 'isEnd' => 0);
}
$preId = $nodeIndex[$firstNode];
//最后一个节点, url值不为空
$endNode = array_pop($arrNode);
//中间节点
if (!empty($arrNode)) {
foreach ($arrNode as $node) {
if (empty($nodeIndex[$node])) {
$nodeIndex[$node] = $i++;
$list[] = array('currentId' => $nodeIndex[$node], 'preId' => $preId, 'name' => $node, 'isEnd' => 0);
}
$preId = $nodeIndex[$node];
}
}
//最后一个节点(文件名)
$list[] = array('currentId' => $i++, 'preId' => $preId, 'name' => $endNode, 'isEnd' => 1);
}
$preId = 1;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。