你好,

我们如何在Drupal 7自定义表单中添加文件上传字段。上传并提交后,文件将保存在/sites/all/default/file中。

谢谢

评论

网络助手的图片

我可以推荐查看示例模块如果您查看有关表单的最后一个教程,它显示了如何上传文件、验证和保存。

form_example_tutorial_10

manoj_j 的图片

嗨,我尝试了 form_example_tutorial_10 中的代码

该文件已上传到默认内的“文件”文件夹中。但出现此错误:

注意:未定义的属性:file_save() 中的 stdClass::$uri(includes\file.inc 的第 575 行)。

PDOException: SQLSTATE[23000]: 完整性约束违规:1062 重复条目 '' 键 'uri': INSERT INTO {file_managed} (filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); drupal_write_record() 中的数组( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => 1 [:db_insert_placeholder_2] => 1334752869 )(includes\common.inc 的第 6776 行)。

你能指点一下吗?

网络助手的图片

你能展示你的代码吗?

从错误消息看来,您似乎是将 uri 放入 file_save() 中,但您应该改用文件对象。

manoj_j 的图片

这里是:

在 hook_menu 下:

$form['file'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#description' => t('上传文件,允许扩展名:jpg , jpeg, png, gif'),
);

在 hook_validation 下:

$file = file_save_upload('file', array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array('png gif jpg jpeg'),
));
if ($file) {
if ($file = file_move($file, 'public://')) {
$form_state['values']['file'] = $file;
}
else {
form_set_error('file', t('无法将上传的文件写入站点的文件夹。'));
}
}
else {
form_set_error('file', t('没有上传文件。'));
}
}

在 hook_submit 下:

$file=$form_state['values']['file'];
取消设置($form_state['values']['file']);
$file->status = FILE_STATUS_PERMANENT;
文件保存($文件);
drupal_set_message(t('表单已提交,图片已保存,文件名:@filename.', array('@filename' => $file->filename)));

网络助手的图片

对我来说很好用。

<?php
function test_menu() {
  $items = array();
  
  $items['test'] = array(
    'title' => 'test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('testform'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function testform($form, &$form_state) {
  $form = array();
  $form['file'] = array(
    	'#type' => 'file',
    	'#title' => t('Image'),
    	'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

function testform_validate($form, &$form_state) {
  $file = file_save_upload('file', array(
  	'file_validate_is_image' => array(),
  	'file_validate_extensions' => array('png gif jpg jpeg'),
  ));
  if ($file) {
    if ($file = file_move($file, 'public://')) {
      $form_state['values']['file'] = $file;
    }
    else {
      form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
    }
  }
  else {
    form_set_error('file', t('No file was uploaded.'));
  }
}

function testform_submit($form, &$form_state) {
  $file=$form_state['values']['file'];
  unset($form_state['values']['file']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
  drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));
}
?>
manoj_j 的图片

谢谢它起作用了!

ajf7688 的图片






<?php
function ajf_multiple_portfolio_images_form($form, &$form_state) {
    $form = array();
    
    $form['photos'] = array(
        '#type' => 'file',
        '#name' => 'files[]',
        '#title' => t('Upload some photos'),
        '#description' => t('JPG\'s, GIF\'s, and PNG\'s only, 10MB Max Size'),
        '#attributes' => array('multiple' => 'multiple'),
    );
    
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Upload'),
    );
    
    return $form;
}

function ajf_multiple_portfolio_images_form_validate($form, &$form_state) {
    
    $num_files = count($_FILES['files']['name']);
    for ($i = 0; $i < $num_files; $i++) {
        $file = file_save_upload($i, array(
            'file_validate_is_image' => array(),
            'file_validate_extensions' => array('png gif jpg jpeg'),
        ));
        if ($file) {
        if ($file = file_move($file, 'public://')) {
          $form_state['values']['photos'][$i] = $file;
        }
        else {
          form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
        }
      }
      else {
        form_set_error('file', t('No file was uploaded.'));
      }    
    }
}

function ajf_multiple_portfolio_images_form_submit($form, &$form_state) {
    
    $count = count($form_state['values']['photos']);
     if (is_array($form_state['values']['photos'])) {
        foreach ($form_state['values']['photos'] as $file) {
            global $user;
            $node = new stdClass;
            $file_info = image_get_info($file->uri);
            $node->type = 'photo';
            $node->title = $file->filename;
            $node->field_image[LANGUAGE_NONE][0]['fid'] = $file->fid;
            $node->field_image[LANGUAGE_NONE][0]['alt'] = $file->filename;
            $node->field_image[LANGUAGE_NONE][0]['title'] = $file->filename;
            $node->field_image[LANGUAGE_NONE][0]['width'] = $file_info['width'];
            $node->field_image[LANGUAGE_NONE][0]['height'] = $file_info['height'];
            $node->field_image[LANGUAGE_NONE][0]['uid'] = $file->uid;
            $node->field_image[LANGUAGE_NONE][0]['filename'] = $file->filename;
            $node->field_image[LANGUAGE_NONE][0]['uri'] = $file->uri;
            $node->field_image[LANGUAGE_NONE][0]['filemime'] = $file->filemime;
            $node->field_image[LANGUAGE_NONE][0]['filesize'] = $file->filesize;
            $node->field_image[LANGUAGE_NONE][0]['status'] = '1';
            $node->field_image[LANGUAGE_NONE][0]['timestamp'] = $file->timestamp;
            $node->field_image[LANGUAGE_NONE][0]['rdf_mapping'] = array();
            $node->uid = $user->uid;
            $node->status = 1;
            $node->active = 1;
            node_save($node);
            }
    }
    drupal_set_message(t("You have uploaded $count photos"));
}
?>


来自  https://www.drupal.org/forum/support/post-installation/2012-04-18/file-upload-in-drupal-7