drupal模块配置过程上传文件

drupal模块配置过程上传文件,drupal,drupal-7,Drupal,Drupal 7,如何处理模块配置部分中的文件上载?这是我到目前为止所拥有的 <?php function dc_staff_directory_admin_settings() { $form['dc_staff_directory_upload_file'] = array( '#type' => 'file', '#title' => t('Upload staff directory excel (.xls) file'), '#description' =

如何处理模块配置部分中的文件上载?这是我到目前为止所拥有的

<?php
function dc_staff_directory_admin_settings() 
{
  $form['dc_staff_directory_upload_file'] = array(
    '#type' => 'file',
    '#title' => t('Upload staff directory excel (.xls) file'),
    '#description' => t('Uploading a file will replace the current staff directory'),
  );
  $form['#submit'][] = 'dc_staff_directory_process_uploaded_file';
  return system_settings_form($form);
}

function dc_staff_directory_process_uploaded_file($form, &$form_state)
{
   //What can I do here to get the file data?
}

如果您使用
managed_文件
类型,Drupal将为您完成大部分处理,您只需在提交功能中将文件标记为永久存储:

function dc_staff_directory_admin_settings() {
  $form['dc_staff_directory_upload_file'] = array(
    '#type' => 'managed_file',
    '#title' => t('Upload staff directory excel (.xls) file'),
    '#description' => t('Uploading a file will replace the current staff directory'),
    '#upload_location' => 'public://path/'
  );

  $form['#submit'][] = 'dc_staff_directory_process_uploaded_file';
  $form['#validate'][] = 'dc_staff_directory_validate_uploaded_file';
  return system_settings_form($form);
}

function db_staff_directory_validate_uploaded_file($form, &$form_state) {
  if (!isset($form_state['values']['dc_staff_directory_upload_file']) || !is_numeric($form_state['values']['dc_staff_directory_upload_file'])) {
    form_set_error('dc_staff_directory_upload_file', t('Please select an file to upload.'));
  }
}

function dc_staff_directory_process_uploaded_file($form, &$form_state) {
   if ($form_state['values']['dc_staff_directory_upload_file'] != 0) {
      // The new file's status is set to 0 or temporary and in order to ensure
      // that the file is not removed after 6 hours we need to change it's status
      // to 1.
      $file = file_load($form_state['values']['dc_staff_directory_upload_file']);
      $file->status = FILE_STATUS_PERMANENT;
      file_save($file);
   }

}
validate函数可能也是一个好主意,显然,如果文件不是必填字段,您就不需要它

这主要是从
image\u示例
模块中获取的,该模块是的一部分。如果您确实不想使用
managed_文件
类型,请查看同一集合中的
file_示例
模块,其中有如何上载非托管文件的示例


希望这对您有所帮助

如果您使用
托管_文件
类型,Drupal将为您完成大部分处理,您只需在提交功能中将文件标记为永久存储:

function dc_staff_directory_admin_settings() {
  $form['dc_staff_directory_upload_file'] = array(
    '#type' => 'managed_file',
    '#title' => t('Upload staff directory excel (.xls) file'),
    '#description' => t('Uploading a file will replace the current staff directory'),
    '#upload_location' => 'public://path/'
  );

  $form['#submit'][] = 'dc_staff_directory_process_uploaded_file';
  $form['#validate'][] = 'dc_staff_directory_validate_uploaded_file';
  return system_settings_form($form);
}

function db_staff_directory_validate_uploaded_file($form, &$form_state) {
  if (!isset($form_state['values']['dc_staff_directory_upload_file']) || !is_numeric($form_state['values']['dc_staff_directory_upload_file'])) {
    form_set_error('dc_staff_directory_upload_file', t('Please select an file to upload.'));
  }
}

function dc_staff_directory_process_uploaded_file($form, &$form_state) {
   if ($form_state['values']['dc_staff_directory_upload_file'] != 0) {
      // The new file's status is set to 0 or temporary and in order to ensure
      // that the file is not removed after 6 hours we need to change it's status
      // to 1.
      $file = file_load($form_state['values']['dc_staff_directory_upload_file']);
      $file->status = FILE_STATUS_PERMANENT;
      file_save($file);
   }

}
validate函数可能也是一个好主意,显然,如果文件不是必填字段,您就不需要它

这主要是从
image\u示例
模块中获取的,该模块是的一部分。如果您确实不想使用
managed_文件
类型,请查看同一集合中的
file_示例
模块,其中有如何上载非托管文件的示例

希望有帮助