Attaching image files to nodes programmatically in Drupal 7

前端 未结 8 1515
耶瑟儿~
耶瑟儿~ 2020-12-07 14:07

Is it possible to add an image to a node programmatically?

相关标签:
8条回答
  • 2020-12-07 14:30

    This works for me:

    define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);
    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    
    $node = node_load(99);
    $filename = 'image.txt';
    chdir(DRUPAL_ROOT);
    $image = file_get_contents('http://www.ibiblio.org/wm/paint/auth/gogh/gogh.white-house.jpg');
    $file = file_save_data($image, 'public://' . $filename, FILE_EXISTS_RENAME);
    $node->field_imagen_producto = array(LANGUAGE_NONE => array('0' => (array)$file));
    node_save($node);
    
    0 讨论(0)
  • 2020-12-07 14:32

    Yes, make it part of the $node object when you save it. Save it using node_save().

    0 讨论(0)
提交回复
热议问题