[转]drupal用程序创建node的方法

function createNode($data = array(), $filepath = NULL) {
    //private function,not necessary to read/copy
    //the
    $node = new stdClass;
    $node->type = "story";
    $node->nid = 0;
    $node->uid = 1;
    $node->status = 1;
    $node->promote = 1;
    $node->changed = $node->created = time();
    $node->sticky = 0;
    $node->format=2;
    $node->title = 'demo node';
    $node->body = 'demo content';
    //$node->taxonomy=array(1,2);
    
    foreach($data as $key => $val) {
        $node->$key = $val;
    }
    
    if($filepath) {
        $file=_ufileobj($filepath);
        $filearray=_cck_filepre($file);
        $node->field_image[0]= $filearray;   // 文件字段,手工改动
    }
    node_save($node);
    $nid=$node->nid;
    $node=NULL;
    return $nid;
}

function _ufileobj($filepath) {
// Create a new file record --object
//print_r(ufileobj( file_directory_path()."/test/test1.flv" ));
    $file = new stdClass();
    //$file->fid=NULL;//when upload success , will return
    $file->list = 1;
    //data igore
    $file->uid = $uid;
    $file->filename = basename($filepath);
    $file->filepath = $filepath;
    $file->filemime = file_get_mimetype(basename($filepath));
    $file->filesize = filesize($filepath);
    // You can change this to the UID you want
    
    $file->status = 1;
    $file->timestamp = time();
    $file->new = true;//addition field , seems useless
    
    drupal_write_record('files', $file);
    // file_set_status($file,1);
    //$node->files[$file_obj->fid] = $file_obj;
    return $file;
}

function _cck_filepre($file) {
    //return file array which suits drupal cck file field
    //$file is the result ofufileobj($filepath);
    $filearray=array();
    $filearray[fid]=$file->fid;
    $filearray["list"]=1;
    $filearray[data]=array("description"=>"","alt"=>"","title"=>"");
    $filearray[uid]=1;
    $filearray[filename]=$file->filename;
    $filearray[filepath]=$file->filepath;
    $filearray[filemime]=$file->filemime;
    $filearray[filesize]=$file->filesize;
    $filearray[status]=1;
    $filearray[timestamp]=$file->timestamp;
    return $filearray;
}

转载于:https://www.cnblogs.com/catcat811/archive/2011/02/11/1951642.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值