Attaching image files to nodes programmatically in Drupal 7

前端 未结 8 1532
耶瑟儿~
耶瑟儿~ 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);
    

提交回复
热议问题