1

アップロードフォームの例を含む複数のdrupalページとstackoverflowページを見つけましたが、1つを機能させることができませんでした。ここに、私が使用したすべてのコードを含め、他の人が行ったことをまとめて解析しました。メニュー、フォーム、送信、検証機能が含まれています。アップロードファイルはサイト->デフォルト->ファイルフォルダに保存されず、送信時に送信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 に答える