Aqui está o meu exemplo de trabalho.
$remoteDocPath = 'http://drupal.org/files/issues/druplicon_2.png';
$doc = system_retrieve_file($remoteDocPath, NULL, FALSE, FILE_EXISTS_REPLACE);
$file = drupal_add_existing_file($doc);
$node = new stdClass;
$node->type = 'node_type';
node_object_prepare($node);
$node->field_image[LANGUAGE_NONE]['0']['fid'] = $file->fid;
node_save($node);
function drupal_add_existing_file($file_drupal_path, $uid = 1, $status = FILE_STATUS_PERMANENT) {
$files = file_load_multiple(array(), array('uri' => $file_drupal_path));
$file = reset($files);
if (!$file) {
$file = (object) array(
'filename' => basename($file_drupal_path),
'filepath' => $file_drupal_path,
'filemime' => file_get_mimetype($file_drupal_path),
'filesize' => filesize($file_drupal_path),
'uid' => $uid,
'status' => $status,
'timestamp' => time(),
'uri' => $file_drupal_path,
);
drupal_write_record('file_managed', $file);
}
return $file;
}