[Drupal] How to upload files using custom admin settings form

| | 2 min read

Sometimes the best way to handle variable values on a Drupal site would be with an admin settings form. For example if you want to store different sizes of the same image which has to be displayed in a page, using admin settings form may be more useful as you have the option to change the image whenever desired.

For this, custom admin settings form has to be created with field type as file and copy the image to public folder in submit function.The path to new file can be stored in variables table using variable_set() function and these variables can be retrieved using variable_get() function.

Custom admin settings form can be created by specifying the path in hook_menu as admin/settings/custom. Get a look on the code mentioned below for creating custom admin settings form.


function hook_menu() {
  $menus['admin/settings/custom'] = array(
    'title' => 'Custom Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('custom_admin_settings'),
    'access callback' => 'user_access',
    'access arguments' => array('access administration menu'),
  );
  return $menus;
}

custom_admin_settings() function will handle the form with input fields as file.


function custom_admin_settings() {
  $form['Custom settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Custom settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['Custom settings']['file1'] = array(
    '#type' => 'file',
    '#title' => t('Custom settings for file1.'),
    '#default_value' => variable_get('file1', ''),
  );
   $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

File can be uploaded to a new location using file_save_upload function. This function adds the file to file_managed table which stores it as a temporary file. Returned value from file_save_upload contains the file information that can be stored in form_state array in validate function.


function custom_admin_settings_validate($form, &$form_state) {
  $file1 = file_save_upload('file1');
  if (isset($file1)) {
    // File upload was attempted.
    if ($file1) {
      // The temporary file should be put in form_values so we can save it on submit.
      $form_state['values']['file1_upload'] = $file1;
    }
    else {
      // File upload failed.
      form_set_error('file1', t('The file cannot be uploaded.'));
    }
  }
}

In submit function the image is copied to public folder using file_unmanaged_copy. This function copies the file to public folder and returns the path to new file which is stored in variables table using variable_set function.


function custom_admin_settings_submit($form, &$form_state) {
  $values = $form_state['values'];
  if ($file = $values['file1']) {
    unset($values['file1_upload']);
    $filename = file_unmanaged_copy($file->url);
    $values['file1'] = $filename;
  }
  variable_set('file1', $values['file1']);
}

This will allow you to create a custom admin settings form for uploading a file. Hope this helps.