1

I have a form, in the site's front end, that upon submission creates a new node.

I've been asked to add a file upload form element, so I used FAPIs 'file'.

I overcame some obstacles, and managed to get the file via the $_FILES array since $form_state has no idea that I uploaded a file (weird), and I performed a file_save_upload on it.

The file is now created, in my directory, and in the database as an entry. All is cool.

How on earth do I link it to the node object I am creating? If I try something like this :

$node->field_file_upload[LANGUAGE_NONE][0] = get_object_vars($file);

where $file is the previously saved file, I get this:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'uri':

As if the node_save() that happens one line below tries to recreate the file. I am obviously missing something here, any help?

  • 1
    I'm not sure, but you could do try it from hook_node_presave(). – AjitS Sep 26 '12 at 11:11
  • please tell me the logic behind this, you will save the my head AND the wall :) – magtak Sep 26 '12 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.