服务器怎么做文件外链,WordPress怎么将文章远程外链图片保存到本地服务器 纯代码实现...

很多人用WordPress建站都会采集一些文章,但是很多时候文章里面的图片很多,一个一个的保存再上传很麻烦,一些人为了省事就直接全盘复制粘贴,这种做法看似聪明,其实对后期网站的发展会有很大的问题。

虽然有一些插件能够实现将远程外链图片保存到本地服务器这个功能,但是插件多少都会对网站速度、性能有一定的影响,总之,“能够用代码实现的问题就不要用插件” wordpress必知事项。(当然服务器非常牛逼的,请忘记这句话)

代码实现:

将下面的代码放置在你的主题functions.php文件底部即可。做实验前,请记得备份、备份、备份。

// 自动本地化外链图片

add_filter('content_save_pre', 'auto_save_image');

function auto_save_image($content) {

$upload_path = '';

$upload_url_path = get_bloginfo('url');

//上传目录

if (($var = get_option('upload_path')) !=''){

$upload_path = $var;

} else {

$upload_path = 'wp-content/uploads';

}

if(get_option('uploads_use_yearmonth_folders')) {

$upload_path .= '/'.date("Y",time()).'/'.date("m",time());

}

//文件地址

if(($var = get_option('upload_url_path')) != '') {

$upload_url_path = $var;

} else {

$upload_url_path = bloginfo('url');

}

if(get_option('uploads_use_yearmonth_folders')) {

$upload_url_path .= '/'.date("Y",time()).'/'.date("m",time());

}

require_once ("../wp-includes/class-snoopy.php");

$snoopy_Auto_Save_Image = new Snoopy;

$img = array();

//以文章的标题作为图片的标题

if ( !empty( $_REQUEST['post_title'] ) )

$post_title = wp_specialchars( stripslashes( $_REQUEST['post_title'] ));

$text = stripslashes($content);

if (get_magic_quotes_gpc()) $text = stripslashes($text);

preg_match_all("/ src=(\"|\'){0,}(http:\/\/(.+?))(\"|\'|\s)/is",$text,$img);

$img = array_unique(dhtmlspecialchars($img[2]));

foreach ($img as $key => $value){

set_time_limit(180); //每个图片最长允许下载时间,秒

if(str_replace(get_bloginfo('url'),"",$value)==$value&&str_replace(get_bloginfo('home'),"",$value)==$value){

//判断是否是本地图片,如果不是,则保存到服务器

$fileext = substr(strrchr($value,'.'),1);

$fileext = strtolower($fileext);

if($fileext==""||strlen($fileext)>4)

$fileext = "jpg";

$savefiletype = array('jpg','gif','png','bmp');

if (in_array($fileext, $savefiletype)){

if($snoopy_Auto_Save_Image->fetch($value)){

$get_file = $snoopy_Auto_Save_Image->results;

}else{

echo "error fetching file: ".$snoopy_Auto_Save_Image->error."
";

echo "error url: ".$value;

die();

}

$filetime = time();

$filepath = "/".$upload_path;//图片保存的路径目录

!is_dir("..".$filepath) ? mkdirs("..".$filepath) : null;

//$filename = date("His",$filetime).random(3);

$filename = substr($value,strrpos($value,'/'),strrpos($value,'.')-strrpos($value,'/'));

//$e = '../'.$filepath.$filename.'.'.$fileext;

//if(!is_file($e)) {

// copy(htmlspecialchars_decode($value),$e);

//}

$fp = @fopen("..".$filepath.$filename.".".$fileext,"w");

@fwrite($fp,$get_file);

fclose($fp);

$wp_filetype = wp_check_filetype( $filename.".".$fileext, false );

$type = $wp_filetype['type'];

$post_id = (int)$_POST['temp_ID2'];

$title = $post_title;

$url = $upload_url_path.$filename.".".$fileext;

$file = $_SERVER['DOCUMENT_ROOT'].$filepath.$filename.".".$fileext;

//添加数据库记录

$attachment = array(

'post_type' => 'attachment',

'post_mime_type' => $type,

'guid' => $url,

'post_parent' => $post_id,

'post_title' => $title,

'post_content' => '',

);

$id = wp_insert_attachment($attachment, $file, $post_parent);

$text = str_replace($value,$url,$text); //替换文章里面的图片地址

}

}

}

$content = AddSlashes($text);

remove_filter('content_save_pre', 'auto_save_image');

return $content;

}

function mkdirs($dir){

if(!is_dir($dir)){

mkdirs(dirname($dir));

mkdir($dir);

}

return ;

}

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 = str_replace('>', '>', $string);

$string = preg_replace('/&(#\d;)/', '&\1', $string);

}

return $string;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
网盘文件免费外链 v2.0 使用方法:直接解压到你网站根目录然后运行index.html 使用环境:IIS/ASP.NET/3.5 需要开启file目录可写权限 演示地址:pan.decadework.com 本程序特点易于扩展,伪静态解析格式可以自己修改,各个方面更改都蛮简单的,不会可以问我 ---文件说明: 文件位置:App_Code/DecadeWorkConfig.cs 1.可以设置是否开启IP限制. 2.可以设置限制外链请求和次数. 文件位置:App_Code/FileUpload.cs 1.可以设置是否启用时间格式路径. 2.可以设置提取文件大小. 文件位置:Up_decadework.ashx 1.可以设置提取文件扩展名,然后更改相应的JS. 文件位置:css/decadework.js 1.可以设置上传文件格式,及文件大小和其它设置. ---V2.0更新内容: 1.增加威盘文件提取. 2.增加百度/威盘文件解析(例如:把百度文件地址换成一个直链地址). 3.增加Global.asax全局文件,用于限制请求次数和解析直链返回网盘文件. 4.增加App_Code/DecadeWorkConfig.cs配置文件,用于设置是否开启IP限制和请求次数限制. 5.优化百度提取功能. 6.修复IP拿不到导致上传和提取失效BUG. 7.修复兼容浏览器问题. 8.自定义解析文件类型. ---解析直链例子: 威盘解析后链接格式为: http://pan.decadework.com/vdisk/35345345.jpg 百度解析后链接格式为: http://pan.decadework.com/baidu/35345345_34534535.jpg (以上只是我随便举例的格式,无法访问的) ---注意: 1.最近有很多网友误把本程序提取理解成解析,这个是理解错误的,本程序的提取是指把百度或者威盘上的文件下载到程序所在的服务器,然后构造一个真实的外链, 不是你们想的把文件存储在第三方网盘的,而是真实存储在程序所在服务器里. 2.由于众多网友提出需要解析功能,本程序该版本已更新上,您只需把百度或者威盘的文件共享地址放到本程序,就拿到伪静态的直链地址,这个就是解析,文件存储在第三方,本程序 只链接显示改变. 3.上个版本说打算更新360云盘提取功能的,我在这里说声抱歉,暂时没时间去和360云盘的API对接,所以就把威盘弄上来了,希望大家理解. ---友情提示: 1.如果哪里不会设置或者更改,请到演示地址评论询问. 2.谢谢大家一如既往的支持本程序. 3.程序无版权问题,你想怎么改就怎么改.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值