2011-09-13 86 views
3

我正在尝试基于自定义表单提交创建节点。除了上传的图片之外,一切都很好。以编程方式上传和保存文件到Drupal节点

我可以捕获它们并将它们设置在表单对象缓存中。当我将数据传入创建节点的函数时,出现此错误:

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

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

这是我使用的代码。 $上传的传入,是在前面的步骤从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字段。我在教程中看到了这两个例子,但都没有工作?

+0

我用'$节点 - > field_image [$节点 - >语言] [0]',看** 0 ** –

+0

但有是多个图像,所以[]是我想要的不是一个静态值 – Kevin

+0

正确 - 抱歉从未使用过多个图像.. –

回答

2

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

我也沿着$节点对象在我的形式传递作为一种价值

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

然后在我提交的handler我只是做到以下几点:

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

Kevin,这就是我在Drupal文档的http://drupal.org/node/201594下面的评论中找到的。但我不确定。我也一样,所以请让我知道你发现了什么。

$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); 
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); 
?> 
相关问题