Drupal 6文件上传模块

时间:2013-01-29 03:24:28

标签: php drupal upload drupal-6

我发现了多个带有上传表单示例的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.'));
}
?>

1 个答案:

答案 0 :(得分:1)

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

我发现自第一页以来你做了很多改动。将其复制并更改为最终答案...... 此代码不包含任何关键修复程序。你现在的问题应该是什么。我只是将你的模块与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.'));
}
?>

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