2 typo
source | link
$nid = $node->nid;
$source_node = node_load($nid);
$filename = 'filename.pdf'; //your specific filename 
$dir = 'public://'.$filename; //path to specific file with name
$file_size = filesize($dir);
$source_node->type = 'my_nodetype'; // error if not used

//New file object
$file = new stdClass;
$file->uid = $source_node->uid;
$file->filename = $filename; //your file name;
$file->uri = $dir;
$file->filemime = file_get_mimetype($dir);
// Make it permanent, otherwise it will get deleted later.
$file->status = FILE_STATUS_PERMANENT;
// Save file object to make it a 'managed file'.
$file_obj = file_copy($file, 'public://', FILE_EXISTS_REPLACE);
$file->type = 'document';

$source_node->field_custom_name['und'][0] = array(
 'fid'  => $file_obj->fid,
 'uid'  => $file_obj->uid,
 'filename'  => $file_obj->filename,
 'uri'  => $file_obj->uri,
 'filemime'  => $file_obj->filemime,
 'filesize'  => $file_obj->filesize,
 'status'  => 1,
 'display'  => 1,
 'description'  => '', 
 );

node_save($source_node);
file_usage_add($file_obj, 'file', 'node', $source_node->nid);

The code is lightly modiefied because of errorPDF is hadattached to current node. thanks for help. This code creates a NEW node withIs anypone has the PDF file attachedcode to do the filefield(field_custom_name)same with entitywrapper. Need a little tweak to save to current nodeplease share.

$source_node = node_load($nid);
$filename = 'filename.pdf'; //your specific filename 
$dir = 'public://'.$filename; //path to specific file with name
$file_size = filesize($dir);
$source_node->type = 'my_nodetype';

//New file object
$file = new stdClass;
$file->uid = $source_node->uid;
$file->filename = $filename; //your file name;
$file->uri = $dir;
$file->filemime = file_get_mimetype($dir);
// Make it permanent, otherwise it will get deleted later.
$file->status = FILE_STATUS_PERMANENT;
// Save file object to make it a 'managed file'.
$file_obj = file_copy($file, 'public://', FILE_EXISTS_REPLACE);
$file->type = 'document';

$source_node->field_custom_name['und'][0] = array(
 'fid'  => $file_obj->fid,
 'uid'  => $file_obj->uid,
 'filename'  => $file_obj->filename,
 'uri'  => $file_obj->uri,
 'filemime'  => $file_obj->filemime,
 'filesize'  => $file_obj->filesize,
 'status'  => 1,
 'display'  => 1,
 'description'  => '', 
 );

node_save($source_node);
file_usage_add($file_obj, 'file', 'node', $source_node->nid);

The code is lightly modiefied because of error is had. This code creates a NEW node with the PDF file attached to the filefield(field_custom_name). Need a little tweak to save to current node.

$nid = $node->nid;
$source_node = node_load($nid);
$filename = 'filename.pdf'; //your specific filename 
$dir = 'public://'.$filename; //path to specific file with name
$file_size = filesize($dir);
$source_node->type = 'my_nodetype'; // error if not used

//New file object
$file = new stdClass;
$file->uid = $source_node->uid;
$file->filename = $filename; //your file name;
$file->uri = $dir;
$file->filemime = file_get_mimetype($dir);
// Make it permanent, otherwise it will get deleted later.
$file->status = FILE_STATUS_PERMANENT;
// Save file object to make it a 'managed file'.
$file_obj = file_copy($file, 'public://', FILE_EXISTS_REPLACE);
$file->type = 'document';

$source_node->field_custom_name['und'][0] = array(
 'fid'  => $file_obj->fid,
 'uid'  => $file_obj->uid,
 'filename'  => $file_obj->filename,
 'uri'  => $file_obj->uri,
 'filemime'  => $file_obj->filemime,
 'filesize'  => $file_obj->filesize,
 'status'  => 1,
 'display'  => 1,
 'description'  => '', 
 );

node_save($source_node);
file_usage_add($file_obj, 'file', 'node', $source_node->nid);

PDF is attached to current node. thanks for help. Is anypone has the code to do the same with entitywrapper. please share.

1
source | link

$source_node = node_load($nid);
$filename = 'filename.pdf'; //your specific filename 
$dir = 'public://'.$filename; //path to specific file with name
$file_size = filesize($dir);
$source_node->type = 'my_nodetype';

//New file object
$file = new stdClass;
$file->uid = $source_node->uid;
$file->filename = $filename; //your file name;
$file->uri = $dir;
$file->filemime = file_get_mimetype($dir);
// Make it permanent, otherwise it will get deleted later.
$file->status = FILE_STATUS_PERMANENT;
// Save file object to make it a 'managed file'.
$file_obj = file_copy($file, 'public://', FILE_EXISTS_REPLACE);
$file->type = 'document';

$source_node->field_custom_name['und'][0] = array(
 'fid'  => $file_obj->fid,
 'uid'  => $file_obj->uid,
 'filename'  => $file_obj->filename,
 'uri'  => $file_obj->uri,
 'filemime'  => $file_obj->filemime,
 'filesize'  => $file_obj->filesize,
 'status'  => 1,
 'display'  => 1,
 'description'  => '', 
 );

node_save($source_node);
file_usage_add($file_obj, 'file', 'node', $source_node->nid);

The code is lightly modiefied because of error is had. This code creates a NEW node with the PDF file attached to the filefield(field_custom_name). Need a little tweak to save to current node.