您现在的位置是:首页 > 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内核开发,为门窗制造、定制安装企业打造,通过可视化后台管理系统快速构建品牌官网。自适应设计确保在手机、平板、电脑等设备上均能获得优质浏览体验查看源码
  • (自适应响应式)html5文章资讯新闻博客pbootcms网站模板下载本模板基于PbootCMS系统开发,为新闻资讯、博客类网站设计,特别适合各类文章内容的发布与管理。采用响应式技术,确保在不同设备上都能获得良好的阅读体验。查看源码
  • (自适应)html5导航目录索引工具类网站源码下载本模板基于PbootCMS开发,为站长导航、网址导航类网站设计。采用HTML5+CSS3技术构建,适配PC端和移动端,提供流畅的浏览体验。适用于个人站长、资源导航平台等场景查看源码
  • (自适应响应式)双语LED照明灯饰灯具外贸网站pbootcms源码下载模板采用响应式设计,能自动适应手机、平板和电脑等多种设备屏幕,确保用户在不同设备上都能获得良好的浏览体验。同一后台管理,数据实时同步,操作简便高效。查看源码
  • 帝国cms7.5游戏门户H5小游戏网站模板源码免费下载基于帝国CMS7.5开发的H5游戏门户模板,为在线游戏平台设计。支持PC端与移动端同步生成,打造完整的游戏娱乐体验平台。查看源码
  • 自适应黑色建筑装饰设计公司个人工作室pbootcms模板基于PbootCMS内核开发的黑金风格模板,为建筑装饰、工程设计类企业打造,采用‌开源架构‌,支持跨行业快速适配,核心优势如下: 查看源码
分享笔记 (共有 篇笔记)
验证码:

本栏推荐