【问题标题】:Uploading and saving a file programmatically to Drupal nodes以编程方式上传和保存文件到 Drupal 节点
【发布时间】:2011-09-13 20:11:04
【问题描述】:

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

我可以很好地捕获它们并将它们设置在表单对象缓存中。当我将数据传递给函数来创建节点时,我得到了这个错误:

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

我也多次收到该错误,尽管一次只提交一两张图片。

这是我正在使用的代码。 $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);

我也试过这个:

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

        file_save($upload);

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

  node_save($node);

第二个导致 MySQL 中 URI 字段出现重复键错误。我在教程中看到了这两个示例,但都没有工作?

【问题讨论】:

  • 我用过$node->field_image[$node->language][0],看0
  • 但是有多个图像,所以 [] 是我想要的不是静态值
  • 正确 - 抱歉从未使用过多张图片..
  • 可能是数组混淆 - 尝试明确设置键..

标签: drupal drupal-7 fileapi


【解决方案1】:

对于 Drupal 7,我玩了很多次,发现最好的方法(也是我工作的唯一方法)是使用 Entity metadata wrappers

我使用了这样的托管文件表单元素:

  // 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/',
  );

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

$form['node'] = array('#type' => 'value', '#value' => $node);

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

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

【讨论】:

    【解决方案2】:

    我使用您的代码将文件字段中的文件上传到内容(在我的情况下为“文档”)并且它有效。只需在代码中为 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);
    ?>
    

    【讨论】:

      【解决方案3】:

      Kevin,这就是我在 cmets 下方 http://drupal.org/node/201594 下的 Drupal 文档中找到的内容。但我完全不确定。我也尝试过,所以请告诉我你发现了什么。

      $path = './sites/default/files/test.jpg';
      $filetitle = 'test';
      $filename = 'test.jpg';
      
      $node = new StdClass();
      
      $file_temp = file_get_contents($path);
      $file_temp = file_save_data($file_temp, 'public://' . $filename, FILE_EXISTS_RENAME);
      
      $node->title = $filetitle;
      $node->uid = 1;
      $node->status = 1;
      $node->type = '[content_type]';
      $node->language = 'und';
      $node->field_images = 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
          )
      )
      );
      $node->field_taxonomy = array('und' => array(
      0 => array(
          'tid' => 76
      )
      ));
      node_save($node);
      

      【讨论】:

        猜你喜欢
        • 2013-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多