几周前我不得不做类似的事情,最后从远程文件模块中调整了一些功能,尤其是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()
. 无论如何,希望这会有所帮助并祝你好运。