How do you move a file/image reference attached to a current node to a different node? I found this previous question, but I'm struggling to adapt it to what I want to accomplish.

How can you take a file/image that is attached to one node, and move it to another or create a new node and attach it there?
I can't recommend this as finished code, and I certainly can't vouch for it's safety/usability, but I was able to accomplish this with:

&lt;?php
function testymod_node_presave($node){

    if($node-&gt;type == 'image_gallery'){
        $nl = $node-&gt;language;//to shorten lines

        if(isset($node-&gt;field_image,$node-&gt;field_image[$nl],$node-&gt;field_image[$nl][1])){
            //Leave the first file, but move all the rest to indidivual nodes of their own
            for($i=1;$i&lt;count($node-&gt;field_image[$nl]);$i++){
                $tmpNode = new stdClass();//Create a node object

                $tmpNode-&gt;uid = $node-&gt;uid;    

                //Use the current node data                    
                $tmpNode-&gt;type = $node-&gt;type;
                $tmpNode-&gt;title = $node-&gt;title;
                $tmpNode-&gt;language = $nl;

                //Set some defaults
                node_object_prepare($tmpNode);

                //Add the file
                $tmpNode-&gt;field_image[$nl][0] = (array) unserialize(serialize($node-&gt;field_image[$nl][$i]));//Only way I know to do a deep clone

                $tmpNode = node_submit($tmpNode); // prep for saving

                node_save($tmpNode); // Should have an nid now
            }
            //Remove attachments from the original
            $node-&gt;field_image[$nl] = array_slice($node-&gt;field_image[$nl], 0,1);
        }

    }
}
?&gt;


I may try to develop this into a usable module with a switch in the content type to enable it for a given content type, but I'd need to figure out how to automatically copy any relevant fields over as well.