2

我正在为 Drupal 站点创建批量上传功能。使用 flash 我可以将文件上传到特定的 url,然后处理文件。我想做的不仅仅是上传文件,而是创建一个特定类型的节点,并将文件保存到已使用 CCK 设置的文件字段中。由于这些是音频文件,filefield 处理文件很重要,因此可以使用 getid3 模块提供附加元数据。

现在我浏览了一些代码,因为我无法找到 API 文档,但完全不清楚我应该如何处理这个问题。理想情况下,我可以将文件传递给一个函数,并使用保存节点时返回的数据,但我无法找到该函数。

如果有人有这方面的经验,我会就如何处理这个问题提出一些建议。

4

3 回答 3

8

几周前我不得不做类似的事情,最后从远程文件模块中调整了一些功能,尤其是remote_file_cck_attach_file()函数。它使用field_file_save_file()来自 filefield 模块的函数,这可能是您正在寻找的函数。

就我而言,这些文件是从几个远程位置获取的,并使用file_save_data(). 将它们附加到 CCK 文件字段发生在hook_nodeapi()预保存时,使用以下命令:

public static function attachAsCCKField(&$node, $filepath, $fieldname, $index=0) {
  // Grab the filefield definition
  $field = content_fields($fieldname, $node->type);
  $validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
  $fieldFileDirectory = filefield_widget_file_path($field);
  // This path does not necessarily exist already, so make sure it is available
  self::verifyPath($fieldFileDirectory);
  $file = field_file_save_file($filepath, $validators, $fieldFileDirectory);
  // Is the CCK field array already available in the node object?
  if (!is_array($node->$fieldname)) {
    // No, add a stub
    $node->$fieldname=array();
  }
  $node->{$fieldname}[$index] = $file;
}

$filepath是应该附加的文件的路径,$fieldname是要在节点中使用的文件字段实例的内部名称,并且$index在多个字段条目的情况下是附加文件的基于 0 的索引。

该函数最终位于实用程序类中,因此是 verifyPath() 调用的类语法。该调用只是确保目标目录可用:

public static function verifyPath($path) {
  if (!file_check_directory($path, FILE_CREATE_DIRECTORY)) {
    throw new RuntimeException('The path "' . $path . '" is not valid (not creatable, not writeable?).');
  }
}

这为我做到了 - 其他一切都自动发生在节点保存上。

我还没有使用 getid3 模块,所以我不知道它是否会与这种方式一起使用。此外,我不需要向文件字段添加额外的信息/属性,因此您可能需要在字段数组中添加更多信息,而不仅仅是field_file_save_file(). 无论如何,希望这会有所帮助并祝你好运。

于 2009-10-29T19:30:28.287 回答
1

我已经用 imagefield 做了一些工作,我认为结构必须是正确的,否则它将无法工作。这需要大量的试验和错误。这就是我填充图像字段的内容。

$image['data'] =array(
            'title' => $media_reference_attributes->getNamedItem("source")->value,
            'description' => $description,
            'alt' => "",);
$image['width'] = $width;
$image['height'] = $height;
$image['mimetype'] = $mime_type
$image['uid'] = 1;
$image['status'] = 1;
$image['fid'] = $fid;
$image['filesize'] = $file->filesize;
$image['nid'] = $id;
$image['filename'] = $url;
$image['timestamp'] = $file->timestamp;
$image['filepath'] = $file_path;

希望这个对你有帮助。

于 2009-10-29T12:56:20.353 回答
1

如果您需要查看集成 Flash 上传,您可能需要查看Image FUpload 。

将文件推送到另一台服务器同时仍然通过 Drupal 处理它们听起来有点像 CDN 空间,也许看看CDNCDN2项目中的行为?

如果您找到明确的解决方案,请返回并发布!

于 2009-10-29T16:00:59.810 回答