如何在使用 node_save($node) 创建图像时将图像附加到节点;

2021-12-29 00:00:00 php drupal drupal-7 drupal-6

您好,我正在使用 drupal 7,并尝试通过使用 php 解析数据并稍后使用 node_save($node) 创建节点来尝试从 xml 导入数据.

Hi i am working with drupal 7 and trying to import data from xml by parsing it using php and later on creating nodes with node_save($node).

到目前为止,我已经成功地从没有任何图像的 xml 中创建了节点.我想在导入时将图像附加到节点.

So far i have been succeeded to create nodes from xml without any images. I want to attach image to the node while i am importing it.

我知道 drupal 7 仍处于 alpha 6 中,但它很好.node_save($node) 函数与 drupal 6 中的几乎相同,但略有不同.

I know drupal 7 is still in alpha 6 but its good. node_save($node) function is almost same as in drupal 6 but little bit different.

好的,这是我的代码图像文件路径存储在一个变量中...任何帮助都会很棒...提前致谢

Ok here is my code image file path is stored in a variable...any help would be great..thanks in advance

function main($head, $txt) {
  $nodes = array();
  $nodes[0]['title'] = $head; // node name
  $nodes[0]['body'] = $txt; // body text for the node
  $nodes[0]['teaser'] = '<p>A node</p>';
  $nodes[0]['timestamp'] = 1281765101;
  $nodes[0]['format'] = 2;
  make_nodes($nodes);
}

function make_nodes($nodes) {
  $new_node = $nodes[0];
  $node = new stdClass();
  $node->type = 'article';
  $node->status = 1;
  $node->uid = 1;
  $node->title = $new_node['title'];
  $node->promote = 1;
  $node->created = $new_node['timestamp'];
  $node->timestamp = $new_node['timestamp'];
  $node->changed= $new_node['timestamp'];
  $node->sticky = 0;
  $node->language = 'en';
  $node->body['und'][0]['format'] = 3;
  $node->body['und'][0]['summary'] = $new_node['teaser'];
  $node->body['und'][0]['value'] = $new_node['body'];
  $node->revision = 0;
  node_submit($node);
  node_save($node);
}

推荐答案

HI.在阅读了 10 个小时的文档后,我终于做到了……我在这里包含了我的代码

HI. After reading the documentation for 10 hours i finaly did it...i am including my code here

$uri = 'bird/bird_image.jpg';
$files =  new stdClass();
$files->uid = (isset($local_user->uid) && !empty($local_user->uid)?$local_user->uid:1);
$files->filename = 'bird.jpg';
$files->uri = $uri; 
$files->filemime = file_get_mimetype($uri);
$files->status = 1;
$files->timestamp = $new_node['timestamp'];

file_copy($files);

这就是如何上传文件并上传到drupal 7数据库

thats how one can upload file and to the drupal 7 database

相关文章