discuz 模拟批量上传附件发帖

discuz 模拟批量上传附件发帖

简介

对于很多用discuz做资源下载站来说,一个个上传附件,发帖是很繁琐的过程。如果需要批量上传附件发帖,就需要去模拟discuz 上传附件的流程。

插件地址 http://addon.discuz.com/?@uauc_auto_thread.plugin

模拟上传

discuz 附件逻辑

dz附件储存在一个附件索引表pre_forum_attachment 和一系列分表pre_forum_attachment_0-9 里面,具体是哪个分表工具帖子tid而定。

参考discuz 内部实现可以精简为

$tableid=substr($tid, -1);   //tableid 为附件分表数字 帖子id

附件模拟上传函数

根据以上分析,封装为一个单独的函数

/**
*@desc 添加附件函数,具体操作是模拟discuz正常上传附件功能,返回一个附件id
*@param $file 服务器上面的文件路径
*@param $tid 帖子id
*@param $pid post_id
*@param $dirs 文件夹
*@param $attachment_score 积分
*@return 返回附件id
**/
function add_attachment($file,$tid,$pid,$dirs,$attachment_score){ $file_path=$dirs.'\\'.$file;
//后缀
$attachment='/'.md5(rand_str()).".attach";
$new_file_path='./data/attachment/forum'.$attachment;
$uid=1; //暂时设置为管理员 if(copy($file_path,$new_file_path)){ $tableid=substr($tid, -1); // 分表处理逻辑 $attach=array(
'tid' => $tid ,
'pid' => $pid,
'uid' => $uid,
'tableid' => $tableid, ); $aid=DB::insert('forum_attachment',$attach,true); if($attachment_score==0){
$attachment_info=array( 'aid' => $aid,
'uid' => $uid, //发布者id
'tid' => $tid,
'pid' => $pid,
'dateline' => time(),
'filename' => $file, //文件名称
'filesize' => filesize($new_file_path),
'attachment' => $attachment , ); }else{
$attachment_info=array( 'aid' => $aid,
'uid' => $uid, //发布者id
'tid' => $tid,
'pid' => $pid,
'dateline' => time(),
'filename' => $file, //文件名称
'filesize' => filesize($new_file_path),
'attachment' => $attachment ,
'price' => $attachment_score ,//附件积分 ); } DB::insert('forum_attachment_'.$tableid,$attachment_info,true);
return $aid; } }

批量发帖

实现模拟批量上传附件之后,再来模拟批量发帖。代码参考discuz 内核实现。


$discuz_uid = 1; // uid
$discuz_user = 'admin'; //用户名
$fid = intval($_POST['fid']); //版块id
$typeid = 0;
$subject = substr(strrchr($dirs, '\\'),1); // 帖子标题
$message = $text_content.$word_content.$imgpng_content.$imgjpg_content; //
$timestamp = $_G['timestamp'];
$onlineip = $_G['clientip'];
$ismobile = 4; // if($arr_attachment_file==NULL){
$newthread = array(
'fid' => $fid,
'posttableid' => 0,
'typeid' => $typeid,
'readperm' => '0',
'price' => '0',
'author' => $discuz_user,
'authorid' => $discuz_uid,
'subject' => $subject,
'dateline' => $timestamp,
'lastpost' => $timestamp,
'lastposter' => $discuz_user
);
$tid = C::t('forum_thread')->insert($newthread, true); $subject = addslashes($subject);
$message = addslashes($message);
$pid = insertpost(array(
'fid' => $fid,
'tid' => $tid,
'first' => '1',
'author' => $discuz_user,
'authorid' => $discuz_uid,
'subject' => $subject,
'dateline' => $timestamp,
'message' => $message,
'useip' => $_G['clientip']
));
}else{
$newthread = array(
'fid' => $fid,
'posttableid' => 0,
'typeid' => $typeid,
'readperm' => '0',
'price' => '0',
'author' => $discuz_user,
'authorid' => $discuz_uid,
'subject' => $subject,
'dateline' => $timestamp,
'lastpost' => $timestamp,
'attachment'=>'1',
'lastposter' => $discuz_user
);
$tid = C::t('forum_thread')->insert($newthread, true); $subject = addslashes($subject);
$message = addslashes($message);
$pid = insertpost(array(
'fid' => $fid,
'tid' => $tid,
'first' => '1',
'author' => $discuz_user,
'authorid' => $discuz_uid,
'subject' => $subject,
'dateline' => $timestamp,
'message' => $message,
'attachment'=>'1',
'useip' => $_G['clientip']
));
foreach($arr_attachment_file as $keyes=> $values ){
foreach($values as $file){
//批量添加附件
add_attachment($file,$tid,$pid,$dirs,$attachment_score); }
} }
DB::query("UPDATE pre_forum_forum SET lastpost='$timestamp', threads=threads+1, posts=posts+1, todayposts=todayposts+1 WHERE fid='$fid'", 'UNBUFFERED');
DB::query("UPDATE pre_common_member_count SET threads=threads+1 WHERE uid='$discuz_uid'", 'UNBUFFERED');
DB::query("UPDATE pre_common_member_status SET lastpost='$timestamp' WHERE uid='$discuz_uid'", 'UNBUFFERED');

演示

茫茫多的文件夹

discuz 模拟批量上传附件发帖

帖子列表

![auto_thread.png]discuz 模拟批量上传附件发帖

批量上传附件之后的帖子

![auto-post-2.png]discuz 模拟批量上传附件发帖

上一篇:go中context源码刨铣


下一篇:迅为iTOP-4418/6818开发板MiniLinux下的GPS使用手册