您现在的位置是:首页 > cms教程 > Discuz教程Discuz教程

discuz模拟批量上传附件发帖的方法

代云2025-07-10Discuz教程已有人查阅

导读discuz 模拟批量上传附件发帖简介对于很多用discuz做资源下载站来说,一个个上传附件,发帖是很繁琐的过程。如果需要批量上传附件发帖,就需要去模拟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');

本文标签:

很赞哦! ()

相关源码

  • (自适应响应式)蓝色勘察设计院机构单位商会协会pbootcms模板下载本模板基于PbootCMS系统开发,为勘察设计院所、工程咨询单位设计,特别适合展示设计成果、技术服务和项目案例。采用响应式技术,确保设计图纸和方案以及文案在不同设备上都能清晰展示。查看源码
  • 帝国cms7.5女性护肤搭配美妆潮流网站源码带数据4.5G本模板专为女性美容护肤行业设计,提供美容护肤、发型设计、女性健康、时尚化妆、娱乐新闻、服饰搭配等女性潮流资讯内容展示。采用帝国CMS7.5开发,同步生成电脑端和手机端,满足用户对美容时尚信息的获取需求。查看源码
  • (自适应响应式)投资理财金融机构财务管理pbootcms模板本模板基于PbootCMS系统开发,为投资理财、金融机构等行业设计。采用专业严谨的布局风格,突出金融服务行业特色,适合展示各类理财产品、投资服务和金融资讯。查看源码
  • (自适应响应式)法律咨询律师事务所法务pbootcms源码下载为律师事务所、法律咨询机构设计,特别适合展示法律服务、律师团队和成功案例。采用响应式技术,确保在不同设备上都能提供专业的法律信息展示和咨询服务。查看源码
  • 自适应建材瓷砖卫浴大理石类pbootcms网站模板源码下载为建材瓷砖、卫浴瓷砖企业打造的高端响应式门户模板,基于PbootCMS内核深度开发。采用前沿HTML5自适应架构,无缝兼容手机端触控交互与PC端展示场景。查看源码
  • (自适应)蓝色英文外贸电子科技产品带三级栏目网站模板为外贸企业设计的英文网站模板,基于PbootCMS系统开发。突出多语言支持和国际化布局,三级栏目结构清晰展示产品分类,响应式设计确保更好客户在移动端和PC端获得一致的专业体验。查看源码
分享笔记 (共有 篇笔记)
验证码:

本栏推荐