Skip to content

Instantly share code, notes, and snippets.

@isholgueras
Created July 30, 2012 09:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isholgueras/3205747 to your computer and use it in GitHub Desktop.
Save isholgueras/3205747 to your computer and use it in GitHub Desktop.
Add files tod node programatically in Drupal 7
<?php
$existing_filepath = "/home/nzcodarnoc/sites/default/files/imported/picture.jpg"
$new_filepath = "public://picture.jpg"
// Load a node
$node = node_load($nid, NULL, TRUE);
// Create the file object
$drupal_file = file_save_data(file_get_contents($existing_filepath), $new_filepath);
$drupal_file->alt = $node->title;
$drupal_file->title = $node->title;
// Assign the file object to the node, as an array
$node->field_my_file[$node->language][0] = get_object_vars($drupal_file);
// Save the node
$node_save($node);
?>
@rafinskipg
Copy link

Great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment