Mapping existing file against a new content type I am facing a situation here.

I have a content type A with an image field(field_upload_snaps) in it. There is another content type B which also contains an image field(field_valid_snaps).

Now user will upload the pics from A content type. So if user upload 3 pics via A then 3 FID and their corresponding URIs will be created in file_managed table in Drupal 7.

Now I have a requirement where I have to insert 2 images out of 3 programatically using Drupal code. Now there is no need to generate any new image copy in public URI as images already exist via A.

I tried two approaches:-

1) I have created a new node of Type B. I tried to fetch file URI,filesize,filemime and all relevant parameter from existing fid and associate with the node using this.

 global $user;
 $node = new stdClass();
 $node-&gt;type = "user_slideshow_snaps";
 $node-&gt;language = LANGUAGE_NONE;
 node_object_prepare($node);
 $node-&gt;uid = $user-&gt;uid;
 $file = (object) array(
  'uid' =&gt; $user-&gt;uid,
  'uri' =&gt; 'public://1.jpeg" ,
  'filemime' =&gt; 'image/jpeg',
  'status' =&gt; 1,
 ); 

 $node-&gt;field_upload_snaps['und'][] = (array)$file;
 $node = node_submit($node); // Prepare node for a submit
 node_save($node);


But it gives me an error that "Cant insert duplicate value in file_managed". It will give because such value already exist in file_managed via A. So I am not able to save value.

2) I saved node first and then manually insert in the image field like this.

 global $user;
 $node = new stdClass();
 $node-&gt;type = "user_slideshow_snaps";
 $node-&gt;language = LANGUAGE_NONE;
 node_object_prepare($node);
 $node-&gt;uid = $user-&gt;uid;
 $node = node_submit($node); // Prepare node for a submit
 node_save($node);

 db_insert('field_data_field_valid_snaps')
    -&gt;fields(array(     
    'entity_type' =&gt; 'node',
    'bundle' =&gt; 'user_actualD_snaps',
    'deleted'=&gt; 0,
    'entity_id'=&gt;$node-&gt;nid,
    'delta'=&gt;0,
    'field_valid_snaps_fid'=&gt;517,
    'field_valid_snaps_alt'=&gt;'',
    'field_valid_snaps_title'=&gt;'',
    'field_valid_snaps_width'=&gt;200,
    'field_valid_snaps_height'=&gt;300, 
    ))
    -&gt;execute();


But the line after node_save() doesnt execute in this case. So I am not able to save it. 

Are there any functions available which will map existing images into another table?
I think the File Field sources module will solve your problem.  (The D7 version works directly with the core Field module).  It will, among other things, allow you to re-use files by an auto-complete textfield.

Btw: Any file appearing in file_managed can only appear once in that table, so you should not be allowed to do what you try to do in your first approach.