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