Last active
March 10, 2018 12:51
-
-
Save fr33m44/118d9f3873dbf959228f06bfe5a0253a to your computer and use it in GitHub Desktop.
将外链图片保存到Typecho本地
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment