Add file to node field

Public

Load file and add it to node field.

Get raw version
php
  1. // Get node for update
  2. $node = node_load('NODE_NID_TO_LOAD');
  3. $final_file_name = 'FILE_NAME_TO_ADD_TO_A_FIELD.EXT';
  4. // Get file content
  5. $file_content = file_get_contents('NAME_OF_FILE_TO_READ');
  6. // Temporary save file
  7. $file = file_save_data($file_content, 'temporary://' . $final_file_name, FILE_EXISTS_REPLACE);
  8. // Add required fields
  9. $file->display = 1;
  10. // Add new item for the field.
  11. $node->MY_FIELD_NAME[LANGUAGE_NONE][] = (array) $file;
  12. // Add/Replace first item of the field.
  13. // $node->MY_FIELD_NAME[LANGUAGE_NONE][0] = (array) $file;
  14. node_save($node);