将外链图片保存到Typecho本地

<?php 
//connect to typecho database
$curl = curl_init();
$res = mysql_connect('localhost', 'root', '1111');
$ret = mysql_select_db('typecho', $res);
$sql = "select * from jq_contents where type = 'post' order by cid asc";
$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);

Gist: https://gist.github.com/fr33m44/118d9f3873dbf959228f06bfe5a0253a

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

通过contents.parent字段与contents.cid字段关联。

之所有没有做成Typecho插件形式,那是因为。。。我不懂Typecho插件机制。。。= =#

标签: PHP, Typecho

已有 2 条评论

  1. Lucky Lucky

    你好这段代码 应该家在哪里。。

    1. 保存成一份php,在本地AMP环境下运行。

添加新评论