Programmatically upload a file to a custom file field
The following code works for me.

$image_url = 'http://test.com/misc/druplicon.png';    
$file_info = system_retrieve_file($image_url, 'public://documents/', TRUE);

if ($file_info-&gt;fid) {
  $node-&gt;field_custom_file['und'][0]['fid'] = $file_info-&gt;fid;
}

$node_ready = node_submit($node);
node_save($node_ready);