4

I'm trying to assign (not upload) files to nodes programmatically.

This is the scenario:

I will create 2000 nodes and there are 2000 PDF files (one PDF per node). I don't want to upload these files via Drupal interface. I will upload all these PDF files via FTP.

These PDF files are well-named, I mean all of them have a standardized name such as {field_magazine_name}-{field_issue_number}.pdf (under sites/default/files/pdf for example).

What I want to do is to assign these PDF files to related nodes while creating/saving the nodes.


Possible Method 1

One method to do this would be to use the File Field Sources module. enter image description here

I think remote file transfer can be used with some JavaScript code. The remote file transfer input can be automatically filled with the fields I want and it will work (for example http://example.com/sites/default/files/pdf/{field_magazine_name}-{field_issue_number}.pdf)... but I don't create nodes manually, I'm creating the nodes via Feeds. So, using JavaScript isn't the method I can use.

Is there any way to programmatically assign (attach) a file to file field while creating/saving the node?

  • I think this is a dup if someone has time to dig. – mpdonadio Sep 22 '15 at 18:16
4

I had a similar situation. My situation is attach a file to a node in the creation of a different content type node. Done this in hook_node_insert

  1. Initially i uploaded all files into a temp directory in /default/files/.
  2. Then for each node, i created a new file object with my file attributes.
  3. Then moved that particular file into the destination where i want the files to be placed. This results a clean complete file object with fid.
  4. Then simply assign this file object attributes to file field and save the node.

The code used is follows..

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

//New file object
$file = new stdClass;
$file->uid = $source_node->uid;
$file->filename = $filename;
$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_move($file, 'public://newpath', FILE_EXISTS_REPLACE);

$source_node->your-file-field-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); 

Hope this will help you..

2

Add file / image fields

// Some file on our system
$file_path = drupal_realpath('somefile.png'); // Create a File object
$file = (object) array(
  'uid' => 1,
  'uri' => $file_path,
  'filemime' => file_get_mimetype($file_path),
  'status' => 1,
); 
$file = file_copy($file, 'public://'); // Save the file to the root of the files directory. You can specify a subdirectory, for example, 'public://images' 
$node->field_image[LANGUAGE_NONE][0] = (array)$file; //associate the file object with the image field:

For more details

  • Just an obvious reminder: The original file is still left (in the example above, "somefile.png" with the full path to it as $file_path). You may want to delete it afterwards with unlink($file_path) – reedbert Nov 2 '18 at 3:18
  • Also, consider setting 'uid' => $GLOBALS['user']->uid, instead if it's going into an file that was composed by the current user. – reedbert Nov 2 '18 at 3:24
1

In addition to Abin: also set the file usage by adding this code after the node save:

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

See the drupal docs.

One more thing apart from this: it is best practice to use LANGUAGE_NONE instead of 'und'.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.