Saving nodes with a filefield

后端 未结 3 2021
一向
一向 2021-01-15 01:38

I\'m in the progress of creating a bulk upload function for a Drupal site. Using flash I\'m able to upload the files to a specific url that then handles the files. What I wa

相关标签:
3条回答
  • 2021-01-15 02:03

    I have done something whith imagefield which worked, I think the structure has to be right otherwise it won't work. It took a lot of trial and error. This is is what I populated the imagefield with.

    $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;
    

    Hope this is of some help.

    0 讨论(0)
  • 2021-01-15 02:06

    I had to do something similar some weeks ago and ended up adapting some functionality from the Remote File module, especially the remote_file_cck_attach_file() function. It uses the field_file_save_file() function from the filefield module, which might be the function you're looking for.

    In my case, the files are fetched from several remote locations and stored temporarily using file_save_data(). Attaching them to a CCK filefield happens on hook_nodeapi() presave, using the following:

    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 is the path to the file that should be attached, $fieldname is the internal name of the filefield instance to use within the node and $index would be the 0 based index of the attached file in case of multiple field entries.

    The function ended up within a utility class, hence the class syntax for the verifyPath() call. The call just ensures that the target directory is available:

    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?).');
      }
    }
    

    That did it for me - everything else happens on node saving automatically.

    I have not used the getid3 module yet, so I have no idea if it would play along with this way of doing it. Also, I had no need to add additional information/attributes to the filefield, so maybe you'd have to put some more information into the field array than just the file returned by field_file_save_file(). Anyways, hope this helps and good luck.

    0 讨论(0)
  • 2021-01-15 02:18

    You might want to look at Image FUpload if you need a look at integrating the flash upload.

    To push the files on to another server while still handling them through Drupal sounds a little like the CDN space, maybe look at the behavior in the CDN or CDN2 projects?

    If you find a clear solution please come back and post it!

    0 讨论(0)
提交回复
热议问题