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