连续复制
一键复制
一键打包
<?php
define('dir', __DIR__);
$dirArr = explode('/', dir);
$md = dir . '/' . $dirArr[count($dirArr) - 1] . '.md';
if (is_file($md))
    unlink($md);
// 生成md文件
// file_put_contents($md, listdir(dir, '', 0));
$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); // 对键排序
// var_dump($listArr);die;
// asort($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;
        }
    }
}
/**
 * 获取占位符
 * @param $level
 * @param string $placeholder
 * @return string
 */
function getPlaceholder($level, $placeholder = '    ')
{
    for ($i = 0; $i < $level; $i++) {
        $placeholder .= $placeholder;
    }
    return substr($placeholder, 0, strlen($placeholder) - 1);
}
/**
 * 获取文件后戳
 * @param $filename
 * @return bool|string
 */
function getExt($filename)
{
    $pos = strrpos($filename, '.');
    $ext = substr($filename, $pos);
    return $ext;
}