如何将许多现有文件与 drupal 文件字段相关联? [英] How can I associate many existing files with drupal filefield?

查看:27
本文介绍了如何将许多现有文件与 drupal 文件字段相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器上已经存储了许多来自静态网站的 mp3 文件,我们现在正在转向 drupal.我将为每个音频文件创建一个节点,但我不想再次上传每个文件.我宁愿将文件复制到我想要它们的 drupal 文件目录中,然后将节点与适当的文件相关联.

I have many mp3 files stored on my server already from a static website, and we're now moving to drupal. I'm going to create a node for each audio file, but I don't want to have to upload each file again. I'd rather copy the files into the drupal files directory where I want them, and then associate the nodes with the appropriate file.

关于如何实现这一目标的任何想法?

Any ideas on how to accomplish that?

谢谢!

推荐答案

我不确定我是否要提出不同的方法,或者我是否准备用不同的词来说明您在原始问题中的意思,但是由于您希望节点成为文件,我宁愿从文件开始生成节点,而不是将现有节点与现有文件相关联.

I am not sure if I am going to propose a different approach or if I am about to tell with different words what you already meant with your original question, but as you want the nodes to be the files, I would rather generate the nodes starting from the files, rather than associating existing nodes with existing files.

一般来说,我会以编程方式执行此操作:对于导入目录中的每个现有文件,我将构建 $node 对象,然后调用 node_save($node) 以将其存储在 Drupal 中.

In generic terms I would do it programmatically: for each existing files in your import directory I would build the $node object and then invoke node_save($node) to store it in Drupal.

当然,在构建 $node 对象时,您需要调用用于管理文件的模块的 API 函数.这是我为完成类似任务而编写的一些示例代码.在这种情况下,我将产品表附加到产品(具有附加字段的节点),因此...

Of course in building the $node object you will need to invoke the API function of the module you are using to manage the files. Here's some sample code I wrote to do a similar task. In this scenario I was attaching a product sheet to a product (a node with additional fields), so...

  • field_sheet 是产品节点中产品表的 CCK 字段
  • product 是节点类型
  • $sheet_file 是产品表文件的完整引用(路径 + 文件名).
  • field_sheet was the CCK field for the product sheet in the product node
  • product was the node type
  • $sheet_file was the complete reference (path + filename) to the product 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);

顺便说一句:如果您使用库来读取 MP3 元数据,您可以以合理的方式设置 $node->title 和其他属性.

BTW: if you use a library to read MP3 metadata, you could set $node->title and other attributes in a sensible way.

希望这有帮助!

这篇关于如何将许多现有文件与 drupal 文件字段相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆