Typecho Wiki
每一个作品都值得被记录

Typecho 代码实现将外链图片保存到本地

Typecho维基君Typecho教程 • 6108次浏览 • 发布 2018-07-17 • 更新 2018-07-17
极致加速的V2Ray 助您畅享全球网络 & 搬瓦工VPS最新优惠码
🛜自用大流量超低月租手机卡推荐榜单 #拒绝流量焦虑

在服务器网站根目录下新建PHP文件,复制以下代码到你新建的文件内【注意:该文件会操作你的数据库,所以在操作前一定记得备份你的数据库,切记!!!】:

<?php 
//connect to typecho database
$curl = curl_init();
$res = mysql_connect('localhost', 'root', '1111');//数据库信息修改为你自己的数据库账号信息
$ret = mysql_select_db('typecho', $res);//typecho修改为你自己的数据库名称
$sql = "select * from jq_contents where type = 'post' order by cid asc";//jq_contents修改为你的数据库中的contents表名称
$query = mysql_query($sql, $res);
while($row = mysql_fetch_assoc($query))
{
    
    preg_match_all('/src="(http:\/\/ww[1234].*?\/.*?\/(.*?))".*?"/', $row['text'], $matches);//匹配规则
    //url : $matches[1][x]  filename : $matches[2][x] , alt: $matches[3][x]
    foreach($matches[1] as $k=>$pic)
    {
        //print_r($matches);
        //文章日期->local date
        $local_date =  date('Y-m-d', $row['created']);
        //print_r($local_date);
        //建立图片所在日期规格的目录
        $arr_date = explode('-', $local_date);
        //@mkdir(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]); 
        //@mkdir(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1]);
        //print_r(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1].'<br />');
        //下载图片
        /*
            curl_setopt($curl, CURLOPT_URL, $matches[1][$k]);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,0);
            $data = curl_exec($curl);
            $ret = file_put_contents(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1]."\\".$matches[2][$k], $data);
            print_r(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1]."\\".$matches[2][$k]." ret:$ret<br />");
        
        */
        //插入typecho数据库
        /*
        $type = explode('.',$matches[2][$k]);
        $text = array(
            'name'=>$matches[2][$k],
            'path'=>"/usr/uploads/".$arr_date[0]."/".$arr_date[1]."/".$matches[2][$k],
            'size'=>filesize(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1]."\\".$matches[2][$k]),
            'type'=> $type[1],
            'mime'=>mime_content_type(dirname(__FILE__)."\\usr\\uploads\\".$arr_date[0]."\\".$arr_date[1]."\\".$matches[2][$k])
        );
        $sql2 = sprintf("INSERT INTO `jq_contents` (  `title`, `slug`, `created`, `modified`, `text`, `order`, `authorId`, `template`, `type`, `status`, `password`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`, `parent`) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", $matches[2][$k],str_replace('.','-', $matches[2][$k]), $row['created'],$row['created'],serialize($text), $k, 1, '', 'attachment', 'publish', '', 0, 1,1,1,$row['cid']);
        $query2 = mysql_query($sql2, $res);
        print_r($sql2.'<br />');
        
        */
        //更新文章内的图片路径
        
        $old_img_path = $matches[1][$k];
        $new_img_path = "http://tunps.com/usr/uploads/".$arr_date[0]."/".$arr_date[1]."/".$matches[2][$k];
        $newtext = str_replace($old_img_path, $new_img_path, $row['text']);
        $newtext = mysql_escape_string($newtext);
        $sql3 = "UPDATE `jq_contents` SET  `text` =  '$newtext' where cid=$row[cid] ";
        $query3 = mysql_query($sql3);
        var_dump($query3);
         
    }
}
curl_close($curl);

以上PHP脚本的作用是将外链的图片通过cURL下载到Typecho本地博客目录。然后在数据库中替换文章内容中的图片链接为本地图片链接。同时将图片信息关联到文章上。

广告声明:文内含有的对外跳转链接(包括不限于超链接、二维码、口令等形式),用于传递更多信息,节省甄选时间,结果仅供参考,Typecho.Wiki所有文章均包含本声明。
本文检索关键词:Typecho教程
厂商投放

【腾讯云】🎉五一云上盛惠!云服务器99元/月续费同价!

腾讯云五一劳动节海量产品 · 轻松上云!云服务器首年1.8折起,买1年送3个月!超值优惠,性能稳定,让您的云端之旅更加畅享。快来腾讯云选购吧!

广告
添加新评论 »

仅有一条评论 »

  1. 看不懂了 者注释很迷 好像 把代码都注释掉了

    。。