代码之家  ›  专栏  ›  技术社区  ›  googletorp

使用文件字段保存节点

  •  2
  • googletorp  · 技术社区  · 17 年前

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

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

    3 回复  |  直到 8 年前
        1
  •  8
  •   Henrik Opel    17 年前

    几周前,我不得不做一些类似的事情,并最终从 Remote File module ,尤其是 remote_file_cck_attach_file() field_file_save_file()

    在我的例子中,这些文件是从几个远程位置获取的,并使用 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 要在节点和中使用的filefield实例的内部名称 $index

    该函数最终位于一个实用程序类中,因此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模块,所以我不知道它是否会与这种方式配合使用。另外,我不需要向filefield添加额外的信息/属性,因此可能需要在field数组中添加更多的信息,而不仅仅是返回的文件 . 不管怎样,希望这对你有帮助,祝你好运。

        2
  •  1
  •   Jeremy French    17 年前

    我已经用imagefield做了一些工作,我认为结构必须是正确的,否则它不会工作。这需要大量的尝试和错误。这就是我在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;
    

        3
  •  1
  •   Grayside    17 年前

    你可能想看看 Image FUpload 如果你需要看一下集成flash上传。

    CDN 或 CDN2