1

我发现了多个带有上传表单示例的 drupal 和 stackoverflow 页面,但我无法使用其中的一个。在这里,我包含了我使用的所有代码,将其他人所做的事情一起解析。我已经包含了一个菜单、一个表单、一个提交和验证功能。上传文件不保存在站点 --> 默认 --> 文件夹中,提交时不显示提交 set_message。为什么这不起作用?

<?php

function upload_example_menu() {
  //$items = array();
  $items['upload_example'] = array(
    'title' => t('Upload'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('upload_example_form'),
    'description' => t('uploading'),
    "access callback" => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function upload_example_form() {
  $form['#attributes'] = array('enctype' => "multipart/form-data");
  $form['upload'] = array('#type' => 'file');
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );

  return $form;
}

function upload_example_form_validate($form, &$form_state) {
  if(!file_check_upload('upload')) {    
    form_set_error('upload', 'File missing for upload.');
  }
}

function upload_example_form_submit($form, &$form_state) {
    $validators = array();
    $file = file_save_upload('upload', $validators, file_directory_path());
    file_set_status($file, FILE_STATUS_PERMANENT);
    drupal_set_message(t('The form has been submitted.'));
}
?>
4

1 回答 1

2

抱歉,我在答案中添加了很多评论,但无法将它们收集到这样的回复中。

我看到你从第一页开始做了很多改变。复制并将其更改为最终答案...此代码不包含任何关键修复。你现在的问题应该是有效的。我只是将您的模块与我为 Drupal 6 拥有的模块进行了比较。但是它需要对最佳实践进行一些更改。请参阅内联注释。

<?php
function upload_example_menu() {
  $items = array();
  $items['upload_example'] = array(
    'title' => 'Upload', // You don't use t() for menu router titles. See 'title callback' that defaults to t().
    'page callback' => 'drupal_get_form',
    'page arguments' => array('upload_example_form'),
    'description' => t('uploading'),
    'access arguments' => array('administer nodes'), // Users with administer nodes permission can access this form. Change it to a suitable one other than setting it TRUE blindly.
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function upload_example_form() {
  $form['#attributes'] = array('enctype' => "multipart/form-data"); // Not necessary for D7.
  $form['upload'] = array(
    '#type' => 'file',
    '#title' => t('File'), // this is usually necessary.
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'), // t()
  );

  return $form;
}

function upload_example_form_validate($form, &$form_state) {
  if(!file_check_upload('upload')) {    
    form_set_error('upload', t('File missing for upload.')); // t()
  }
}

function upload_example_form_submit($form, &$form_state) {
    $validators = array();
    $file = file_save_upload('upload', $validators, file_directory_path());
    file_set_status($file, FILE_STATUS_PERMANENT);
    drupal_set_message(t('The form has been submitted.'));
}
?>

让我们知道它是怎么回事:)

于 2013-01-29T13:39:42.940 回答