user hook node update to attach files to a node? im using the following code to upload an array of files when a node gets saved.

function mymodule_node_update($node){
 $result = $query;

 foreach ($result as $record) {
$url = ($record['src']);
$name_arr = explode('/', $url );
$filename = end($name_arr);
$filepath = system_retrieve_file($url, 'public://my_files/');

$file = new StdClass();
$file-&gt;uid = 1;
$file-&gt;uri = $filepath;
$file-&gt;filename = $filename;
$file-&gt;filemime = file_get_mimetype($file-&gt;uri);
$file-&gt;status = 1;  

$dest = file_default_scheme() . '://'.$filename; 
$file = file_copy($file, $dest); 
$file-&gt;display = 1;
$file-&gt;description = "";
 }
}


This uploads the files to my public directory, and creates an entry into files_managed in my database.

But now how do I link those files to a filefield on the node im updating? 
I've tried variations of

  $node-&gt;field_file = (array)$file;


but nothing is working.

Thanks for any help
The field data has already been committed by the time hook_node_update() is called, so you won't be able to make any persistent alterations to field data in there.

Instead you could use hook_node_presave(), which is run before any data is written to the backend.

function mymodule_node_presave($node) {
  // Build up file object

  // Then add it to the node.
  $node-&gt;field_files[$node-&gt;language][] = (array)$file;
}