分享 1 0

    typecho全站静态化方案

    实现

    利用wget全站保存为html,然后再修改文件中的链接

    步骤

    把以下代码保存为html.php

    <?php
    $url = 'https://blog.asbid.cn'; //网址,不能以"/"结尾
    $rurl=''; //要替换成路径或网址,可为空,不能以"/"结尾
    $dir = __DIR__ . "/" . str_replace('https://', '', str_replace('http://', "", $url));
    exec("clear",$clc);
    echo $clc[0];
    echo "开始下载文件\r\n";
    exec("rm -rf {$dir}",$return);
    exec("wget -r -p -np {$url}",$return);
    
    $dirs = get_filenamesbydir($dir);
    
    //不处理非html文件
    for ($i = 0; $i < count($dirs); $i++) {
        $file=str_replace(__DIR__, "", $dirs[$i]['file']);
    
        if (!preg_match("/html/",$file)  ) {
            //删除对应的元素
            unset($dirs[$i]);
        
        }
      
    }
    array_filter($dirs);
    sort($dirs);//重新生成索引下标
    
    //网址处理
    $count=count($dirs);
    for ($i = 0; $i < $count; $i++) {
        $content=str_replace($url,$rurl,file_get_contents($dirs[$i]['file']));
        file_put_contents($dirs[$i]['file'],$content);
        $n=$i+1;
        exec("clear",$clc);
        echo $clc[0];
        echo "文件下载完毕\r\n";
        echo "开始处理文件,共{$count}个文件需要处理,已处理{$n}个\r\n";
    
    }
    echo "处理完毕,文件目录:{$dir}\r\n";
    
    
    function get_allfiles($path, &$files)
    {
        if (is_dir($path)) {
            $dp = dir($path);
            while ($file = $dp->read()) {
                if ($file !== "." && $file !== "..") {
                    get_allfiles($path . "/" . $file, $files);
                }
            }
            $dp->close();
        }
        if (is_file($path)) {
            $files[] = ['file' => $path];
        }
    }
    function get_filenamesbydir($dir)
    {
        $files = array();
        get_allfiles($dir, $files);
        return $files;
    }
    

    然后执行

    php html.php
    老孙
    Your description here.