How to link many existing files to drupal file field?

I have many mp3 files stored on my server already from a static website, and now we move on to drupal. I am going to create a node for each audio file, but I do not want to download each file again. I would rather copy the files to the drupal file directory where I want them, and then link the nodes to the corresponding file.

Any ideas on how to do this?

Thanks!

+2
source share
2 answers

I'm not sure if I’m going to suggest a different approach, or if I’m going to tell you in different words what you already had in mind with your original question, but since you want the nodes to be files, I would rather generate the nodes starting from the files, rather than linking existing nodes to existing files.

In general terms, I would do this programmatically: for every existing file in your import directory, I would build an object $nodeand then run node_save($node)it to save it to Drupal.

Of course, when creating the object, $nodeyou will need to call the API function of the module that you use to manage the files. Here is an example of the code that I wrote to perform a similar task. In this case, I attached the product sheet to the product (a node with additional fields), so ...

  • field_sheet CCK node
  • product node
  • $sheet_file - ( + ) .

, :

// Load the CCK field
$field = content_fields('field_sheet', 'product');
// Load the appropriate validators
$validators = array_merge(filefield_widget_upload_validators($field));
// Where do we store the files?
$files_path = filefield_widget_file_path($field);
// Create the file object
$file = field_file_save_file($sheet_file, $validators, $files_path);
// Apply the file to the field, this sets the first file only, could be looped
// if there were more files
$node->field_scheda = array(0 => $file);
// The file has been copied in the appropriate directory, so it can be
// removed from the import directory
unlink($sheet_file);

BTW: MP3, $ node → .

, !

+4

file_import module , ( node ), API- Drapal, , .

0

Source: https://habr.com/ru/post/1724107/


All Articles