连续复制
一键复制
复制代码
  1. <?php
  2. define('dir', __DIR__);
  3. $dirArr = explode('/', dir);
  4. $md = dir . '/' . $dirArr[count($dirArr) - 1] . '.md';
  5. if (is_file($md))
  6. unlink($md);
  7. // 生成md文件
  8. // file_put_contents($md, listdir(dir, '', 0));
  9. $notExt = ['.exe', '.doc', '.docx', '.pdf', '.sh~', '.rar', '.zip', '.tar','.gif','.jpg','.png','.mod'];
  10. $listArr = listdir(dir);
  11. ksort($listArr, SORT_STRING | SORT_FLAG_CASE | SORT_NATURAL); // 对键排序
  12. // var_dump($listArr);die;
  13. // asort($listArr, SORT_STRING | SORT_FLAG_CASE | SORT_NATURAL); // 对值排序
  14. $content = getContent($listArr);
  15. $tree = "###目录###" . PHP_EOL . "```package" . PHP_EOL . listTree($listArr) . PHP_EOL . '```';
  16. file_put_contents($md, $content . PHP_EOL . $tree);
  17. function getContent($arr, $level = 0)
  18. {
  19. global $notExt;
  20. $content = '';
  21. foreach ($arr as $k => $v) {
  22. if (is_array($v)) {
  23. $content .= '###' . getPlaceholder($level, '#') . $k . "###" . getPlaceholder($level, '#') . PHP_EOL;
  24. $content .= getContent($v, $level + 1);
  25. } else if(!in_array(getExt($v),$notExt)){
  26. echo getExt($v);
  27. $fileArr = explode('/', str_replace('\\', '/', $v));
  28. $file = $fileArr[count($fileArr) - 1];
  29. $content .= '###' . getPlaceholder($level, '#') . $file . "###" . getPlaceholder($level, '#') . PHP_EOL;
  30. if (strtolower(getExt($v))==='.md') {
  31. $content .= str_replace('.', '', getExt($file)) . PHP_EOL .mb_convert_encoding( file_get_contents($v), 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' ). PHP_EOL ;
  32. }else{
  33. $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;
  34. }
  35. }
  36. }
  37. return $content;
  38. }
  39. function listTree($arr, $level = 0)
  40. {
  41. $content = '';
  42. $placeholder = getPlaceholder($level);
  43. foreach ($arr as $k => $v) {
  44. if (is_array($v)) {
  45. $content .= $placeholder . '└── ' . $k . PHP_EOL;
  46. $content .= listTree($v, $level + 1);
  47. } else {
  48. $fileArr = explode('/', str_replace('\\', '/', $v));
  49. $file = $fileArr[count($fileArr) - 1];
  50. $content .= $placeholder . '├── ' . $file . PHP_EOL;
  51. }
  52. }
  53. return $content;
  54. }
  55. function listdir($dir)
  56. {
  57. global $notExt;
  58. //定义一个数组
  59. $files = array();
  60. $notInArr = ['.', '..', '.git', '.idea', '.DS_Store', 'MD', 'MD.php', '.gitignore', '.gitattributes', 'LICENSE'];
  61. //检测是否存在文件
  62. if (is_dir($dir)) {
  63. //打开目录
  64. if ($handle = opendir($dir)) {
  65. //返回当前文件的条目
  66. while (($file = readdir($handle)) !== false) {
  67. //去除特殊目录
  68. if (!in_array($file, $notInArr)) {
  69. //判断子目录是否还存在子目录
  70. if (is_dir($dir . "/" . $file)) {
  71. // 递归调用本函数,再次获取目录
  72. $files[$file] = listdir($dir . "/" . $file);
  73. } else {
  74. if (in_array(getExt($file), $notExt))
  75. continue;
  76. // 获取目录数组
  77. $files[] = $dir . "/" . $file;
  78. }
  79. }
  80. }
  81. //关闭文件夹
  82. closedir($handle);
  83. //返回文件夹数组
  84. return $files;
  85. }
  86. }
  87. }
  88. /**
  89. * 获取占位符
  90. * @param $level
  91. * @param string $placeholder
  92. * @return string
  93. */
  94. function getPlaceholder($level, $placeholder = ' ')
  95. {
  96. for ($i = 0; $i < $level; $i++) {
  97. $placeholder .= $placeholder;
  98. }
  99. return substr($placeholder, 0, strlen($placeholder) - 1);
  100. }
  101. /**
  102. * 获取文件后戳
  103. * @param $filename
  104. * @return bool|string
  105. */
  106. function getExt($filename)
  107. {
  108. $pos = strrpos($filename, '.');
  109. $ext = substr($filename, $pos);
  110. return $ext;
  111. }