[WordPress] WordPress自动保存远程图片(2.3.X系列测试通过)

Feb 13th, 2008

转载本站文章请注明,转载自:扶凯[http://www.php-oa.com]

本文链接: http://www.php-oa.com/2008/02/13/wordpresszidongbaocunyuanchengtupian.html

转:http://www.rongdede.com/archives/16.html

网络上比较流行的是老鬼的做法.我在其基础上针对最新版做了些修改.

自动保存远程图片修改方法如下:

1、在wp-admin/edit-form-advanced.php 第167行

HTML 代码:


<span id=”autosave”></span>


后面加增加

HTML 代码:


Save pic:<input type=”checkbox” name=”savepic” value=”checked” />


2、修改 wp-admin/post.php,将第7行的

PHP 代码:


wp_reset_vars(array(’action’, ’safe_mode’, ’withcomments’, ’posts’,

’content’, ’edited_post_title’, ’comment_error’, ’profile’, ’trackback_url’,

excerpt’, ’showcomments’, ’commentstart’, ’commentend’, ’commentorder’));


改为

PHP 代码:


wp_reset_vars(array(’action’, ’safe_mode’, ’withcomments’, ’posts’, ’content’,

edited_post_title’, ’comment_error’, ’profile’, ’trackback_url’, ’excerpt’, ’showcomments’,

’commentstart’, ’commentend’, ’commentorder’, ’savepic’));



3、在wp-admin/includes/post.php的开头增加

PHP 代码:


function open($file,$type=”){
global $fromurl,$referer;
$cachename=$file;
if($type){
$file=$fromurl.’/’.$type.’/’.$file;
}else{
$file=$fromurl.$file;
}
if($open=file($file)){
$count=count($open);
for($i=0;$i<$count;$i++){
$theget.=$open[$i];
}
}else{
die(’请求过多,超时,请刷新’);
}
return $theget;
}

function cut($file,$from,$end){

$message=explode($from,$file);
$message=explode($end,$message[1]);
return        $message[0];
}
function dhtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = dhtmlspecialchars($val);
}
} else {
$string = str_replace(’&’, ’&’, $string);
$string = str_replace(’”‘, ’”‘, $string);
$string = str_replace(’<’, ’<’, $string);
$string = str_replace(’>’, ’>’, $string);
$string = preg_replace(’/&(#\d;)/’, ’&\1′, $string);
}
return $string;
}
function random($length) {
$hash = ”;
$chars = ’ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz’;
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}

function mkdirs($dir)
{
if(!is_dir($dir))
{
mkdirs(dirname($dir));
mkdir($dir);
}
return ;
}


再在

PHP 代码:


$_POST[’post_content’] = $_POST[’content’];


的后面添加以下代码:
注意,上面的代码有两个地方有,第一个是修改的,最后一个是添加的.你如果只加添加的功能就在最后那个加,如果修改的也要加,就在前面的也加上.

PHP 代码:


if($_POST[’savepic’]==”checked”){
// begin to save pic;
$img_array = array();
$content1 = stripslashes($_POST[’post_content’]);
if (get_magic_quotes_gpc()) $content1 = stripslashes($content1);
//echo $content1;//文章内容嘿嘿开始处理了
preg_match_all(”/(src|SRC)=\”(http:\/\/(.+).(gif|jpg|jpeg|bmp|png))/isU”,$content1,$img_array);//正则开始匹配所有的图片并放入数据
$img_array = array_unique(dhtmlspecialchars($img_array[2]));

print_r($img_array);
set_time_limit(0);

foreach ($img_array as $key => $value) {
$get_file = file_get_contents($value);//开始获取图片了哦
$filetime = time();
$filepath = ”../wp-content/uploads/pic2/”.date(”Y”,$filetime).”/”.date(”m”,$filetime).”/”;//图片保存的路径目录
!is_dir($filepath) ? mkdirs($filepath) : null;
$filename = date(”YmdHis”,$filetime).random(1).’.’.substr($value,-3,3);
$fp = @fopen($filepath.$filename,”w”);
@fwrite($fp,$get_file);
fclose($fp);//完工,哈
$content1 = preg_replace(”/”.addcslashes($value,”/”).”/isU”, ”/wp-content/uploads/pic2/”. date(”Y”,$filetime).”/”.date(”m”,$filetime).”/”.$filename, $content1);   // 顺便替换一下文章里面的图片地址

}

$_POST[’post_content’] = AddSlashes($content1); //老鬼的旧版本没有这个转义过程所以会出错

// end save pic;
}


完毕.现在,你再发表文章时,提交按钮旁边多了个选项选中”save pic”,再点”发表”,即可保存远程图片了.总结一下,要修改的文件有:

wp-admin/edit-form-advanced.php
wp-admin/post.php
wp-admin/includes/post.php

注:要打开了PHP.INI的output_buffering 和allow_url_fopen

Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪 ViVi 365Key 网摘 天极网摘 和讯网摘 博拉网 POCO 网摘 饭否 QQ 书签 Digbuzz 我挖网 Mister Wong
  1. 明年今日
    Feb 17th, 2008 at 03:07
    #1