以编程方式将文件上载并保存到Drupal节点

Kev*_*vin 4 drupal drupal-7 fileapi

我正在尝试基于自定义表单提交创建节点.一切都很好,除了上传的图像.

我可以捕获它们并将它们设置在表单对象缓存中.当我将数据传递给函数来创建节点时,我收到此错误:

"无法复制指定的文件,因为不存在该名称的文件.请检查您是否提供了正确的文件名."

我也多次收到错误,尽管一次只提交一个或两个图像.

这是我正在使用的代码.$ uploads是传入的,是上一步中从file_save_upload()返回的文件对象数组:

if (isset($uploads)) {
    foreach ($uploads as $upload) {
      if (isset($upload)) {
        $file = new stdClass;
        $file->uid = 1;
        $file->uri = $upload->filepath;
        $file->filemime = file_get_mimetype($upload->uri);
        $file->status = 1;  

        $file = file_copy($file, 'public://images');

        $node->field_image[$node->language][] = (array) $file;
      }
    }
  }

  node_save($node);
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

if (isset($uploads)) {
    foreach ($uploads as $upload) {
        $upload->status = 1;  

        file_save($upload);

        $node->field_image[$node->language][] = (array) $upload;
      }
    }
  }

  node_save($node);
Run Code Online (Sandbox Code Playgroud)

第二个导致URI字段中的MySQL出现重复键错误.我在教程中看到过这两个例子,但两个都没有用?

小智 5

我使用你的代码将文件字段中的文件上传到内容(在我的情况下为"文档")并且它已经工作了.只需在代码中为field_document_file'display'添加一个值即可.这是我使用的确切脚本:

<?php
// Bootstrap Drupal
define('DRUPAL_ROOT', getcwd());
require_once './includes/bootstrap.inc';
require_once './includes/file.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Construct the new node object.
$path = 'Documents/document1.doc';
$filetitle = 'test';
$filename = 'document1.doc';

$node = new StdClass();

$file_temp = file_get_contents($path);

//Saves a file to the specified destination and creates a database entry.
$file_temp = file_save_data($file_temp, 'public://' . $filename, FILE_EXISTS_RENAME);

$node->title = $filetitle;
$node->body[LANGUAGE_NONE][0]['value'] = "The body of test upload document.\n\nAdditional Information";
$node->uid = 1;
$node->status = 1;
$node->type = 'document';
$node->language = 'und';
$node->field_document_files = array(
'und' => array(
    0 => array(
        'fid' => $file_temp->fid,
        'filename' => $file_temp->filename,
        'filemime' => $file_temp->filemime,
        'uid' => 1,
        'uri' => $file_temp->uri,
        'status' => 1,
        'display' => 1
    )
)
);
$node->field_taxonomy = array('und' => array(
0 => array(
    'tid' => 76
)
));
node_save($node);
?>
Run Code Online (Sandbox Code Playgroud)


mas*_*ief 5

对于 Drupal 7,我玩了很多,发现最好的方法(也是我工作的唯一方法)是使用实体元数据包装器

我使用了一个托管文件表单元素,如下所示:

  // Add file upload widget
  // Use the #managed_file FAPI element to upload a document.
  $form['response_document'] = array(
    '#title' => t('Attach a response document'),
    '#type' => 'managed_file',
    '#description' => t('Please use the Choose file button to attach a response document<br><strong>Allowed extensions: pdf doc docx</strong>.'),
    '#upload_validators' => array('file_validate_extensions' => array('pdf doc docx')),
    '#upload_location' => 'public://my_destination/response_documents/',
  );
Run Code Online (Sandbox Code Playgroud)

我还将表单中的 $node 对象作为值传递

$form['node'] = array('#type' => 'value', '#value' => $node);
Run Code Online (Sandbox Code Playgroud)

然后在我的提交处理程序中,我只需执行以下操作:

  $values = $form_state['values'];
  $node = $values['node'];
  // Load the file and save it as a permanent file, attach it to our $node.
  $file = file_load($values['response_document']);
  if ($file) {
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);

    // Attach the file to the node.
    $wrapper = entity_metadata_wrapper('node', $node);
    $wrapper->field_response_files[] = array(
      'fid' => $file->fid,
      'display' => TRUE,
      'description' => $file->filename,
    );
    node_save($node);
  }
Run Code Online (Sandbox Code Playgroud)