[support] Programatically create a node and add file attachment

Kamal Palei palei.kamal at gmail.com
Tue Oct 9 06:25:03 UTC 2012


Hi All
I want to create a node and attach a file programatically in D7.
I have below code, it creates a node, but does not have any attachment in
it.
Can somebody point out whats the wrong here.

*create_node_attach_file($filename , $fid)
{
    $node = new stdClass();
    $node->type = 'page';
    node_object_prepare($node);*

*    $node->language = LANGUAGE_NONE;
    $node->title = 'Title - XYZ';
    $node->uid = 1;   *

*    $absfilename = 'sites/default/files/' . $user->uid . '/' . $filename';
    $filepath = drupal_realpath($absfilename);*

*    $file = (object) array(
        'uid' => 1,
        'uri' => $filepath,
        'filemime' => file_get_mimetype($filepath),
        'status' => 1,
        );*

*    $file->new = true;
    $file->fid = $fid;*

*    // Attach the file object to your node
    $node->field_resume[$node->language][0] = (array)$file;*

*    // Prepare node for saving
    $node = node_submit($node);*

*    //save the node
    node_save($node);

}*

Thanks
Kamal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20121009/a180c399/attachment.html 


More information about the support mailing list