2

I've got a hundred or so files (mixed types: pdf, ppt, doc, etc). I want to be able to upload these to my server, and then point my D7 site to the directory on the server that contains all of these files so that the site automatically creates a node for each file, with a single, different file attached to each node. Is there a module that will do this for me?

1 Answer 1

2

Using Bulk File Nodes you can create unlimited number of nodes at once .

Bulk File Nodes allows a user to upload or import many files at once, and have many individual nodes created, one for each file . Where this module significantly differentiates from other modules like Bulk Media Upload is that after uploading/importing the file(s), the user is given the opportunity to edit all of the fields for each node in a single form.

This module creates a node for each file and you can edit the fields of each node after uploading.

enter image description here


Solution 1

You are right, Bulk File Nodes works only with images. since you need to create nodes programmatically and assign the files to the nodes you can use the following code to create a node without a file field

$uid = SOME_USER_UID
$values = array(
        'type' => 'THE_CONTENT_TYPE',
        'uid' => $uid,
        'status' => 0,
        'comment' => 0,
        'promote' => 0,
        'language'  => 'en'
);
$entity = entity_create ('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity); 
$ewrapper->title->set('THE_TITLE');

$ewrapper->save();

But from Attaching image files to nodes programmatically in Drupal 7 you can use the following code to attach the file to the above node

$filepath = drupal_realpath('misc/druplicon.png');
 // Create managed File object and associate with Image field.
 $file = (object) array(
'uid' => 1,
'uri' => $filepath,
'filemime' => file_get_mimetype($filepath),
'status' => 1,
  );

// We save the file to the root of the files directory.
$file = file_copy($file, 'public://');

$ewrapper ->field_file[LANGUAGE_NONE][0] = (array)$file;

This code should be above $ewrapper->save(); line.

Solution 2

You can also create the nodes without file fields as described above. and for each node with $node variable Programmatically Attach Files to Node.

// We have complete file path.
$file_path = '/whatever/path/to/file/sample.pdf';

$file = new stdClass;
$file->uid = $node->uid;
$file->filename = pathinfo($file_path, PATHINFO_BASENAME);
$file->uri = $file_path;
$file->filemime = file_get_mimetype($file_path);
// Make it permanent, otherwise it will get deleted later.
$file->status = FILE_STATUS_PERMANENT;

// Save file object to make it a 'managed file'.
file_save($file);

// We will save file under this location.
$destination = 'public://PDFs/';

// Make sure destination directory exists before moving files to that path.
// This will create directory if it does not exists.
file_prepare_directory($destination, FILE_CREATE_DIRECTORY);

// Now attach file to the file field.
$node->field_pdf[$node->language][0] = (array) $file;

// Default file type field comes with this extra properties.
$node->field_pdf[$node->language][0]['display'] = 1;
$node->field_pdf[$node->language][0]['description'] = 'Our special PDF';

// Save node to make the attachment effective.
node_save($node);
6
  • Thank you! I found this module on drupal.org, but was put off by the line in the description: "Currently, the module only allows for uploading/importing images, but plans are under way to extend this functionality to other filetypes." Is it still the case that the module will only upload image files?
    – Jim
    Commented Jan 27, 2016 at 17:07
  • Did you try Multi Node Add module?
    – M a m a D
    Commented Jan 27, 2016 at 18:29
  • I've had a look at that module, but while it lets me create multiple nodes it doesn't seem to let me do that based on a list of files. Am I missing something?
    – Jim
    Commented Jan 27, 2016 at 18:42
  • @Jim I updated my answer
    – M a m a D
    Commented Jan 27, 2016 at 19:00
  • That's fantastic, thank you so much! I'd just started putting the scaffolding together for a custom module to do this, so it's great that I won't have to work out how to create all the nodes etc programatically. If I get time I'll release my module to contrib and will, of course, credit you if I do that. Thanks again! :)
    – Jim
    Commented Jan 27, 2016 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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