复制代码
- <?php
- define('dir', __DIR__);
- $dirArr = explode('/', dir);
- $md = dir . '/' . $dirArr[count($dirArr) - 1] . '.md';
- if (is_file($md))
- unlink($md);
-
-
- $notExt = ['.exe', '.doc', '.docx', '.pdf', '.sh~', '.rar', '.zip', '.tar','.gif','.jpg','.png','.mod'];
- $listArr = listdir(dir);
- ksort($listArr, SORT_STRING | SORT_FLAG_CASE | SORT_NATURAL);
-
-
- $content = getContent($listArr);
- $tree = "###目录###" . PHP_EOL . "```package" . PHP_EOL . listTree($listArr) . PHP_EOL . '```';
- file_put_contents($md, $content . PHP_EOL . $tree);
- function getContent($arr, $level = 0)
- {
- global $notExt;
- $content = '';
- foreach ($arr as $k => $v) {
- if (is_array($v)) {
- $content .= '###' . getPlaceholder($level, '#') . $k . "###" . getPlaceholder($level, '#') . PHP_EOL;
- $content .= getContent($v, $level + 1);
- } else if(!in_array(getExt($v),$notExt)){
- echo getExt($v);
- $fileArr = explode('/', str_replace('\\', '/', $v));
- $file = $fileArr[count($fileArr) - 1];
- $content .= '###' . getPlaceholder($level, '#') . $file . "###" . getPlaceholder($level, '#') . PHP_EOL;
- if (strtolower(getExt($v))==='.md') {
- $content .= str_replace('.', '', getExt($file)) . PHP_EOL .mb_convert_encoding( file_get_contents($v), 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' ). PHP_EOL ;
- }else{
- $content .= '```' . str_replace('.', '', getExt($file)) . PHP_EOL .mb_convert_encoding( file_get_contents($v), 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' ). PHP_EOL . '```' . PHP_EOL;
-
- }
- }
- }
- return $content;
- }
- function listTree($arr, $level = 0)
- {
- $content = '';
- $placeholder = getPlaceholder($level);
- foreach ($arr as $k => $v) {
- if (is_array($v)) {
- $content .= $placeholder . '└── ' . $k . PHP_EOL;
- $content .= listTree($v, $level + 1);
- } else {
- $fileArr = explode('/', str_replace('\\', '/', $v));
- $file = $fileArr[count($fileArr) - 1];
- $content .= $placeholder . '├── ' . $file . PHP_EOL;
- }
- }
- return $content;
- }
- function listdir($dir)
- {
- global $notExt;
-
- $files = array();
- $notInArr = ['.', '..', '.git', '.idea', '.DS_Store', 'MD', 'MD.php', '.gitignore', '.gitattributes', 'LICENSE'];
-
- if (is_dir($dir)) {
-
- if ($handle = opendir($dir)) {
-
- while (($file = readdir($handle)) !== false) {
-
- if (!in_array($file, $notInArr)) {
-
- if (is_dir($dir . "/" . $file)) {
-
- $files[$file] = listdir($dir . "/" . $file);
- } else {
- if (in_array(getExt($file), $notExt))
- continue;
-
- $files[] = $dir . "/" . $file;
- }
- }
- }
-
- closedir($handle);
-
- return $files;
- }
- }
- }
-
- function getPlaceholder($level, $placeholder = ' ')
- {
- for ($i = 0; $i < $level; $i++) {
- $placeholder .= $placeholder;
- }
- return substr($placeholder, 0, strlen($placeholder) - 1);
- }
-
- function getExt($filename)
- {
- $pos = strrpos($filename, '.');
- $ext = substr($filename, $pos);
- return $ext;
- }
评论已关闭