I would like to create an instance of a file object manually and use file_save() to commit it (e.g. using an image file):
global $user; $file = new stdClass; $file->uid = $user->uid; $file->filename = 'image.png'; $file->uri = 'public://path/to/file/image.png'; $file->status = 1; $file->filemime = 'image/png'; file_save($file);
Then you must call file_usage_add() so that Drupal knows that your module has an interest in this file (using nid from your $node ):
file_usage_add($file, 'mymodule', 'node', $node->nid);
Finally, you can add the file to node:
$node->field_file[$node->language][] = array( 'fid' => $file->fid );
Hope that helps
Clive source share