Discuz模擬批量上傳附件發帖


簡介

對於很多用discuz做資源下載站來說,一個個上傳附件,發帖是很繁瑣的過程。如果需要批量上傳附件發帖,就需要去模擬discuz 上傳附件的流程。

模擬上傳

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');

轉自:http://www.blogs8.cn/posts/Eryae77  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM