Created
July 30, 2012 09:04
-
-
Save isholgueras/3205747 to your computer and use it in GitHub Desktop.
Add files tod node programatically in Drupal 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great