01 | function createNode($data = array(), $filepath = NULL) { |
02 | //private function,not necessary to read/copy |
03 | //the |
04 | $node = new stdClass; |
05 | $node->type = "story"; |
06 | $node->nid = 0; |
07 | $node->uid = 1; |
08 | $node->status = 1; |
09 | $node->promote = 1; |
10 | $node->changed = $node->created = time(); |
11 | $node->sticky = 0; |
12 | $node->format=2; |
13 | $node->title = 'demo node'; |
14 | $node->body = 'demo content'; |
15 | //$node->taxonomy=array(1,2); |
16 | |
17 | foreach($data as $key => $val) { |
18 | $node->$key = $val; |
19 | } |
20 | |
21 | if($filepath) { |
22 | $file=_ufileobj($filepath); |
23 | $filearray=_cck_filepre($file); |
24 | $node->field_image[0]= $filearray; // 文件字段,手工改动 |
25 | } |
26 | node_save($node); |
27 | $nid=$node->nid; |
28 | $node=NULL; |
29 | return $nid; |
30 | } |
31 |
32 | function _ufileobj($filepath) { |
33 | // Create a new file record --object |
34 | //print_r(ufileobj( file_directory_path()."/test/test1.flv" )); |
35 | $file = new stdClass(); |
36 | //$file->fid=NULL;//when upload success , will return |
37 | $file->list = 1; |
38 | //data igore |
39 | $file->uid = $uid; |
40 | $file->filename = basename($filepath); |
41 | $file->filepath = $filepath; |
42 | $file->filemime = file_get_mimetype(basename($filepath)); |
43 | $file->filesize = filesize($filepath); |
44 | // You can change this to the UID you want |
45 | |
46 | $file->status = 1; |
47 | $file->timestamp = time(); |
48 | $file->new = true;//addition field , seems useless |
49 | |
50 | drupal_write_record('files', $file); |
51 | // file_set_status($file,1); |
52 | //$node->files[$file_obj->fid] = $file_obj; |
53 | return $file; |
54 | } |
55 |
56 | function _cck_filepre($file) { |
57 | //return file array which suits drupal cck file field |
58 | //$file is the result ofufileobj($filepath); |
59 | $filearray=array(); |
60 | $filearray[fid]=$file->fid; |
61 | $filearray["list"]=1; |
62 | $filearray[data]=array("description"=>"","alt"=>"","title"=>""); |
63 | $filearray[uid]=1; |
64 | $filearray[filename]=$file->filename; |
65 | $filearray[filepath]=$file->filepath; |
66 | $filearray[filemime]=$file->filemime; |
67 | $filearray[filesize]=$file->filesize; |
68 | $filearray[status]=1; |
69 | $filearray[timestamp]=$file->timestamp; |
70 | return $filearray; |
71 | } |
没有评论:
发表评论