How can i use settings form http://api.drupal.org/api/drupal/modules%21system%21system.module/function/system_settings_form/7 to make a file saved and retrieved using variable_get() ?

I am using Drupal 7.

2 Answers 2


The settings form does not save entries to a file, but stores the value in the database. It can only be called from a custom module you will have to build.

Below you will find a link to Drupal's own module tutorial. Follow the steps and you will come across the usage of the settings page. (configuration form)



System settings will only save the file temporarily, to keep files permanently you will need to process and save the file yourself with file_save().

You can save the file (fid) with variable_set(), and use variable_get() followed by file_load($fid) to load your file.

Here is a code example (link to source below):

function custom_settings_form($form, &$form_state) {
  $form['files'] = array(
    '#type' => 'fieldset',
    '#title' => t('Files'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  $form['files']['custom_file_1'] = array(
    '#title' => t('File 1'),
    '#type' => 'managed_file',
    '#default_value' => variable_get('custom_file_1', ''),
    '#upload_location' => 'public://custom/',

  // Perform our custom submit before system submit.
  $form['#submit'][] = 'custom_settings_form_submit';
  return system_settings_form($form);

And the custom submit:

function custom_settings_form_submit($form, &$form_state) {
  global $user;
  // Load the file via file fid.
  $file = file_load($form_state['values']['custom_file_1']);
  if ($file) {
    // Change status to permanent.
    $file->status = FILE_STATUS_PERMANENT;
    // Save.
    // Optionally record that the module (e.g. user module) is using the file.
    file_usage_add($file, 'user', 'user', $user->uid);

See https://www.drupal.org/node/1090198 for full explanation and code examples.

  • How can we do the same in Drupal 8
    – Vinodhini
    Commented May 22, 2020 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.