I had to do something similar a few weeks ago and ended up adapting some functions from the Remote File module , especially the function remote_file_cck_attach_file(). It uses a function field_file_save_file()from the file field module, which may be the function you are looking for.
In my case, files are extracted from several remote locations and temporarily saved using file_save_data(). Attaching them to the CCK file field occurs in the hook_nodeapi()presave using the following:
public static function attachAsCCKField(&$node, $filepath, $fieldname, $index=0) {
$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);
self::verifyPath($fieldFileDirectory);
$file = field_file_save_file($filepath, $validators, $fieldFileDirectory);
if (!is_array($node->$fieldname)) {
$node->$fieldname=array();
}
$node->{$fieldname}[$index] = $file;
}
$filepath - , , $fieldname - , node $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?).');
}
}
- node .
getid3, , . , / , , , , , field_file_save_file(). , , .