English 中文(简体)
在Droupal的档案系统中插入一个档案吗?
原标题:Programmatically insert a file in Drupal s file system?

我正在利用CURL挑选一个档案,希望将其在当地拯救,但这样,它会pl入Droupal的档案系统。 我是否必须用人工添加或有更好的办法? 我最接近的是:

function image_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items);

但我不知道如何利用这一点。 任何更好的建议?

问题回答

有若干种方式,但最容易的是使用:

$source =  /path/to/file.ext ;
$dest =  public://uploads/ ; // Or whatever
$file = file_save_upload($source, array(), $dest, FILE_EXISTS_RENAME);

if ($file) {
  // file_save_upload marks the file as temporary, we need to mark as permanent or it will be cleared from the database in a few hours
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
}

This just about drove me out of my mind and in trying to figure out a simple way to circumvent the form API expectations I came across this question. Clive s answer was the beginning of how I figured it out because it looks like you can provide a source as the first arg for file_save_upload() but as it turns out, you cannot. At least not in the current drupal 7.30.

我检查了他提供的联系,并拆除了这一职能。 基本上,即使你走一条完全的源头路,它仍然期待金字塔阵列中第一个飞跃走这条路。 谁知道为什么,但什么是 d。 因此,我这样做:

//after verifying all post fields are set...
$node = new stdClass();
$node->type =  user_files ;
node_object_prepare($node);

$node->title    = $_POST[ title ];
$node->author = 2; //hardcoded because only admin uses this form
$node->uid = 2;
$node->language = LANGUAGE_NONE;
$node->body[$node->language][0][ value ]   = $_POST[ body ];
$node->body[$node->language][0][ summary ] = text_summary($_POST[ body ]);
$node->body[$node->language][0][ format ]  =  filtered_html ;

$node->field_first_name[$node->language][0][ value ] = $_POST[ first_name ];
$node->field_last_name[$node->language][0][ value ] = $_POST[ last_name ];

node_save($node);

if(isset($_FILES[ file ][ tmp_name ]) && $_FILES[ file ][ name ] !=   )
{
  //upload file

  $file = new stdClass();
  $file->uid = 2;
  $file->status = 0;
  $file->filename = trim(drupal_basename($_FILES[ file ][ name ]),  . );
  $file->uri = $_FILES[ file ][ name ];
  $file->filemime = file_get_mimetype($file->filename);
  $file->filesize = $_FILES[ file ][ size ];
  $file->filename = file_munge_filename($file->filename,  jpg jpeg gif png doc docx pdf );
  $file->destination = file_destination( private://  . $file->filename, FILE_EXISTS_RENAME);
  $file->uri = $file->destination;
  if (!drupal_move_uploaded_file($_FILES[ file ][ tmp_name ], $file->uri)) {

    return false; //set errors or do whatever you want on error
  }
  else
  {
    drupal_chmod($file->uri);
    $existing_files = file_load_multiple(array(), array( uri  => $file->uri));
    if (count($existing_files)) {
      $existing = reset($existing_files);
      $file->fid = $existing->fid;
    } 

    if ($file = file_save($file)) {
      // Add file to the cache.

      $node->field_file[$node->language][0][ uri ] =  private:// .$file->filename;
      $node->field_file[$node->language][0][ fid ] = $file->fid;
      $node->field_file[$node->language][0][ display ] = 1;
      $node->field_file[$node->language][0][ description ] =  User uploaded file ;
      node_save($node);

      //do we notify someone?
    }    
  }
}

What this does is creates a node of a specified type. In this case, user_files, then if a file is uploaded, add the file to the media table, or file table or whatever it s called. Then adds the association to the newly created node.

这是否令人怀疑? Yeah。 我为什么使用内部的散装形式APIC? up,这是为什么。 我们并不总是有选择,因此,当客户要求采用一种基本的表格,仅用电子邮件发送,便迅速而容易地通过邮件收发短信或寄信。 然后他们补充说,他们想要档案。 之后,他们想在rup倒的后端上加一,突然出现恐怖的雪球,如果从APIC表格开始,那本来会变得非常容易。 我不知道谁开始这样做,但我不得不结束,我与这辆gha车一样这样做。 希望能帮助你们或目前处于暴动之中的其他人。





相关问题
Drupal Multi-language: Simple strings not translated

I m adding additional languages to a Drupal site that I m building. Getting the translation of content working is fairly easy using the Internationalisation module. Yet, simple things such as date ...

Setting up a WYSIWYG editor for Drupal site users [closed]

Looking through the Drupal contrib modules, and after a few Google searches, it becomes evident that there are any number of choices and combos available to set up a WYSIWYG editor in Drupal. I m ...

Change size of user/password login box

I don t know how to change the size of the login username/password boxes on the drupal site that I m trying to build. I m stumbling through the theming, and don t know where to find the file that ...

How does Drupal provide an edit/review/publish model?

How does Drupal support a means to update and review a website before it is published? Does it only allow you to preview a page at a time before you publish it or is there a way to create a site ...

Term for rotating header

I m looking for terminology that describes this behavior: The header of a web-page contains a different image every time you visit it. Update: It is not an advertisement, but images related to the ...

Has anyone checked out Drupal 7? [closed]

Has anyone checked out a copy of Drupal 7 yet? What do people think? I m pretty excited about the PDO and all of the designers I work with a very excited about the new admin interface/structure. Do ...