百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>Discuz教程> discuz教程:发帖远程图片自动下载到服务器并设置附件原理
分享文章到:

discuz教程:发帖远程图片自动下载到服务器并设置附件原理

发布时间:12/03 来源: 浏览: 关键词:
为了帮助网站内部人员发帖不用把其他网站上的图片保存到本地再上传到服务器。就二次开发了这个功能    不是插件(本人插件技术有限)。

就是发帖的时候,可以直接复制别人的文章。如果别人的文章里边包含图片 ,就自动把图片 下载到服务器,

并且可以设置缩略图,添加到附件列表,diy的时候也可以选择缩略图。

先看下效果吧。





下载完成后,自动发帖。





其实远程图片下载并不难,php自带的文件读取函数就可以。难的是 要明白 dz发帖自带的标签,图片的存储,数据的插入等。

首先缩略图 用到的数据表为:forum_threadimage表。
附件表为:forum_attachment(附件帖子对应表)     forum_attachment1~9(附件存储表)   forum_attachment_unused(临时附件表)

刚开始由于不理解原理,绕了很多弯路,

首先就是  获取message就是 帖子内容。不过dz已经自动将其标签化了,远程图片标签为: 上传的图片附件标签为:
[attachimg]aid[/attachimg]其中aid为附件的id 存储在forum_attachment(附件帖子对应表)中,具体的附件路径存在 forum_attachment1~9(附件存储表)  

所以你要匹配到[img]标签  然后获取 url  下载-存储-插入数据库。其中比较复杂的是  那个附件临时表。下载下来的图片都存储在附件临时表中。
具体发帖的时候  从临时表中读取然后插入到forum_attachment1~9(附件存储表)  ,并且更新forum_attachment(附件帖子对应表)
更新之前



更新之后


这里比较重要的是 aid 这个字段

算了,下班了,明天再更。发一段js代码吧
  1. function validate(theform) {
  2.        
  3.                
  4.         var message = wysiwyg ? html2bbcode(getEditorContents()) : theform.message.value;
  5.         if(!theform.parseurloff.checked) {
  6.                 message = parseurl(message);
  7.         }

  8.         if(($('postsubmit').name != 'replysubmit' && !($('postsubmit').name == 'editsubmit' && !isfirstpost) && theform.subject.value == "") || !sortid && !special && trim(message) == "") {
  9.                 showError('抱歉,您尚未输入标题或内容');
  10.                 return false;
  11.         } else if(mb_strlen(theform.subject.value) > 80) {
  12.                 showError('您的标题超过 80 个字符的限制');
  13.                 return false;
  14.         }
  15.         if(ispicstyleforum == 1 && ATTACHORIMAGE == 0 && isfirstpost) {
  16.         }
  17.         if(in_array($('postsubmit').name, ['topicsubmit', 'editsubmit'])) {
  18.                 if(theform.typeid && (theform.typeid.options && theform.typeid.options[theform.typeid.selectedIndex].value == 0) && typerequired) {
  19.                         showError('请选择主题对应的分类');
  20.                         return false;
  21.                 }
  22.                 if(theform.sortid && (theform.sortid.options && theform.sortid.options[theform.sortid.selectedIndex].value == 0) && sortrequired) {
  23.                         showError('请选择主题对应的分类信息');
  24.                         return false;
  25.                 }
  26.         }
  27.         for(i in EXTRAFUNC['validator']) {
  28.                 try {
  29.                         eval('var v = ' + EXTRAFUNC['validator'][i] + '()');
  30.                         if(!v) {
  31.                                 return false;
  32.                         }
  33.                 } catch(e) {}
  34.         }

  35.         if(!disablepostctrl && !sortid && !special && ((postminchars != 0 && mb_strlen(message) < postminchars) || (postmaxchars != 0 && mb_strlen(message) > postmaxchars))) {
  36.                 showError('您的帖子长度不符合要求。\n\n当前长度: ' + mb_strlen(message) + ' 字节\n系统限制: ' + postminchars + ' 到 ' + postmaxchars + ' 字节');
  37.                 return false;
  38.         }
  39.         if(UPLOADSTATUS == 0) {
  40.                 if(!confirm('您有等待上传的附件,确认不上传这些附件吗?')) {
  41.                         return false;
  42.                 }
  43.         } else if(UPLOADSTATUS == 1) {
  44.                 showDialog('您有正在上传的附件,请稍候,上传完成后帖子将会自动发表...', 'notice');
  45.                 AUTOPOST = 1;
  46.                 return false;
  47.         }
  48.         if(isfirstpost && $('adddynamic') != null && $('adddynamic').checked && $('postsave') != null && isNaN(parseInt($('postsave').value)) && ($('readperm') != null && $('readperm').value || $('price') != null && $('price').value)) {
  49.                 if(confirm('由于您设置了阅读权限或出售帖,您确认还转播给您的听众看吗?') == false) {
  50.                         return false;
  51.                 }
  52.         }
  53.        
  54.         /* if(jQuery('#postsubmit').hasClass('upload_now') ) {
  55.                 showError('正在上传图片中!请稍后!');
  56.                 return false;
  57.         } */
  58.        
  59.         return check_remote_img(message,theform);

  60. }


  61. function theform_(message,theform) {
  62.        
  63.         theform.message.value = message;
  64.        
  65.         if($('postsubmit').name == 'editsubmit') {
  66.                 postsubmit(theform);
  67.                 return false;
  68.         } else if(in_array($('postsubmit').name, ['topicsubmit', 'replysubmit'])) {

  69.                 if(seccodecheck || secqaacheck) {
  70.                        
  71.                         var chk = 1, chkv = '';
  72.                         if(secqaacheck) {
  73.                                 chkv = $('checksecqaaverify_' + theform.sechash.value).innerHTML;
  74.                                 if(chkv.indexOf('loading') != -1) {
  75.                                         setTimeout(function () { validate(theform); }, 100);
  76.                                         chk = 0;
  77.                                 } else if(chkv.indexOf('check_right') == -1) {
  78.                                         showError('验证问答错误,请重新填写');
  79.                                         chk = 0;
  80.                                 }
  81.                         }
  82.                         if(seccodecheck) {
  83.                                 chkv = $('checkseccodeverify_' + theform.sechash.value).innerHTML;
  84.                                 if(chkv.indexOf('loading') !== -1) {
  85.                                         setTimeout(function () { validate(theform); }, 100);
  86.                                         chk = 0;
  87.                                 } else if(chkv.indexOf('check_right') === -1) {
  88.                                         showError('验证码错误,请重新填写');
  89.                                         chk = 0;
  90.                                 }
  91.                         }
  92.                         if(chk) {
  93.                                 postsubmit(theform);
  94.                         }
  95.                 } else {
  96.                        
  97.                         postsubmit(theform);
  98.                 }
  99.                 return false;
  100.         }
  101. }


  102. function check_remote_img(message,theform) {
  103.         var reg_1 = /\[img=\d+,\d+\]([\s\S]*?)\[\/img\]/g;
  104.         var reg_2 = /\[img\]([\s\S]*?)\[\/img\]/g;
  105.         var reg_3 = /\[img=\d+,\d+\]([\s\S]*?)\[\/img\]/;
  106.         var reg_4 = /\[img\]([\s\S]*?)\[\/img\]/;
  107.         var args = new Array();
  108.         args['fade'] = 1;
  109.         args['cover'] = 1;
  110.         if( reg_1.test(message) || reg_2.test(message) ) {
  111.                 var match = new Array();
  112.                 var url_ = new Array();
  113.                 match = message.match(reg_1);
  114.                 match2 = message.match(reg_2);
  115.                 if( match2 == null && match != null ) {
  116.                         match2 = new Array();
  117.                         match2 = match2.concat(match);
  118.                         url = match2;
  119.                         for(i = 0; i < url.length; i++) {
  120.                                 var str = url[i];
  121.                                 url_[i] = reg_3.exec(str)[1];
  122.                         }
  123.                 }
  124.                 else if( match2 != null && match != null ) {
  125.                         match2 = match2.concat(match);
  126.                         url = match2;
  127.                         for(i = 0; i < url.length; i++) {
  128.                                 var str = url[i];
  129.                                 if( reg_3.test(str) ) {
  130.                                         url_[i] = reg_3.exec(str)[1];
  131.                                 }
  132.                                 else {
  133.                                         url_[i] = reg_4.exec(str)[1];
  134.                                 }
  135.                         }
  136.                 }
  137.                 else {
  138.                         match = new Array();
  139.                         match2 = match.concat(match2);
  140.                         url = match2;
  141.                         for(i = 0; i < url.length; i++) {
  142.                                 var str = url[i];
  143.                                 url_[i] = reg_4.exec(str)[1];
  144.                         }
  145.                 }
  146.                
  147.                 var html = '<style>'+
  148.                                         '#uploadRemote {width:530px; height:330px; overflow:hidden;}'+
  149.                                         '#uploadRemote .upload_c {padding:0px 15px 0;}'+
  150.                                         '#fwin_dialog h3.flb span{display:none;}'+
  151.                                         '#message_ {display:none;}'+
  152.                                         '#upload_show {height:300px; overflow-y:auto;}'+
  153.                                         '#upload_show p {width:100px; height:100px; margin:0 auto; margin-top:20px;}'+
  154.                                         '#upload_show ul li {float:left; margin:5px; }'+
  155.                                         '#upload_show ul li a {text-align:center;}'+
  156.                                         '.msg {line-height:24px; color:#369; text-align:center; border:1px dashed #ddd;}'+
  157.                                         '</style>'+
  158.                                         '<div id="uploadRemote">'+
  159.                                                 '<div class="upload_c">'+
  160.                                                         '<div class="msg">发现你的帖子包含<span style="color:red; font-weight:bold;">'+url_.length+'</span>张远程图片<br />系统将为你自动下载!<br /></div>'+
  161.                                                         '<div id="upload_show">'+
  162.                                                         '<p class="loading"><img src="/static/image/common/loading2.gif" /></p>'+
  163.                                                         '<ul></ul>'+
  164.                                                         '</div>'+
  165.                                                 '</div>'+
  166.                                         '</div>';
  167.                 showDialog(html,'info','远程图片下载','',1,args);

  168.                 setTimeout(function(){
  169.                         jQuery('#postsubmit').addClass('upload_now');
  170.                         jQuery('#uploadRemote .msg').html('图片下载中,请耐心等待。如果等待时间过长,请从新刷新页面。谢谢!剩余<span style="color:red ;font-weight:bold;">'+url_.length+'</span>张');

  171.                         i = 0;
  172.                         dois(url_,i,message,theform);
  173.                         return false;
  174.                         jQuery('#upload_show p').hide();
  175.                 },1200);
  176.                
  177.                 return false;
  178.         }
  179.         else {
  180.                 return theform_(message,theform);
  181.         }
  182. }

  183. function dois(url_,i,message,theform) {
  184.         html = '';
  185.         jQuery.ajax({
  186.                 type: 'POST',
  187.                 url: 'forum.php?mod=uploadRemote&action=newthread',
  188.                 data: {url:url_[i],message:message},
  189.                 dataType: "json",
  190.                 success: function (data) {
  191.                         message = data.message;
  192.                         html2bbcode(editdoc.body.innerHTML = message);
  193.                         message = html2bbcode(getEditorContents());
  194.                         if( data.error == 1 ) {
  195.                                 // 图片下载失败!
  196.                                 html += '<li><a href="javascript:;">下载失败</a></li>';
  197.                                 jQuery('#upload_show ul').append(html);
  198.                                 jQuery('#uploadRemote .msg span').text(url_.length - i - 1);
  199.                                 i++;
  200.                                 if( i < url_.length ) {
  201.                                         dois(url_,i,message,theform);
  202.                                 }
  203.                                 else {
  204.                                         setTimeout(function(){return theform_(message,theform);},2000);
  205.                                 }
  206.                         }
  207.                         else {
  208.                                
  209.                                 html += '<li><img src="'+data.tmp_name+'" width="90" height="90" /></li>';
  210.                                 jQuery('#upload_show ul').append(html);
  211.                                 jQuery('#postbox').append('<input type="text" name="attachnew['+data.aid+'][description]" class="px" style="display: none" id="image_desc_'+data.aid+'">');
  212.                                 jQuery('#uploadRemote .msg span').text(url_.length - i - 1);
  213.                                 i++;
  214.                                 if( i < url_.length ) {
  215.                                         dois(url_,i,message,theform);
  216.                                 }
  217.                                 else {
  218.                                        
  219.                                         setTimeout(function(){return theform_(message,theform);},2000);
  220.                                 }
  221.                         }
  222.                 },
  223.         });
  224. }
复制代码validate() 为发帖调用js,我是把匹配都放到js中了   然后通过ajax来下载图片,并且替换标签 把[img]全部替换为附件标签[attachimg]
打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

相关文章

共有5人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板