Form API managed_file doesn't show up I am working with Drupal 7. When I use the following code, nothing shows up in the form; when I change managed_file with file, everything is fine.

$form['mfile'] = array (
  '#type' =&gt; 'managed_file',
  '#title' =&gt; t('File to upload'),
  '#size' =&gt; 22,'#upload_location' =&gt; 'public://vm-files',
);


Do you know why this happens?
You have to set the #default_value (as you would nearly any FAPI element) if you want the current set value to show up on the form when you visit the page. I have code such as the following in my module:

  for($i=1,$z=MAX_FRONTPAGE_IMAGES;$i&lt;=$z;$i++) {
  $form["frontpage_image_fid_$i"] = array(
    '#type'            =&gt; 'managed_file',
    '#upload_location' =&gt; 'public://site_theme_assets/',
    '#default_value'   =&gt; variable_get("frontpage_image_fid_$i", ''),
    '#title'           =&gt; t("Random Image #$i for Frontpage"),
    '#description'     =&gt; t('This image will appear on the frontpage of the  site and have line1 and line2 on top of it.'),
  );
  }


The only gotcha I have when trying to use managed_file is that after save, on edit I get an error about the File status. That's because you should notify the file_usage table that your module owns/uses the file upon upload, eg:

/** callback for fapi admin settings page form that uses managed_file elements */
function foundation_frontpage_admin_submit($form, &amp;$form_state) {
  for($i=1,$z=MAX_FRONTPAGE_IMAGES;$i&lt;=$z;$i++) {
    if ($form_state['values']["frontpage_image_fid_$i"]) {
      $f = $form_state['values']["frontpage_image_fid_$i"];
      // Load the file via file.fid
      $file = file_load($f);
      // Change status to permanent.
      $file-&gt;status = FILE_STATUS_PERMANENT;
      // Save.
      file_save($file);

      // I was receiving an error on Edit of a filled out form/file upload.
      // Managed file: the file used in the field may not be referenced
      // The solution is file_usage_add() to this module as commented here.
      //
      // http://drupal.stackexchange.com/a/4819/3279
      file_usage_add($file, 'foundation', 'variable:frontpage_image_fid_', $i, 1);

      variable_set("frontpage_image_fid_$i", $f);
    } else {
      variable_set("frontpage_image_fid_$i", NULL);
    }
  }
  drupal_set_message('Changes saved for frontpage.');
}