Is there a difference in Drupal 7 to add a file-upload field on a "normal" form (i.e. a separate page for a modules' configuration) as opposed to the small settings area for a CCK field (the one that appears after clicking on the cog for the field you'd like to modify under the "Manage Display" tab)?

I have a bunch of configuration options for a custom field using hook_field_formatter_settings_form, but am stuck at trying to figure out how to create a file upload field. I'd ideally like to have the user able to browse for an image and upload it without having the page reload. This is all I currently have:

$element['custom_field_name']['image'] = array(
    '#type' => 'file',
    '#size' => 48,
    '#title' => t('Image upload'),
    '#description' => t("Upload an image."),

If it is possible to have a file upload field (that uses ajax) in this area, what else must I do?


My file field now looks like this:

$element['custom_field_name']['image'] = array(
    '#type' => 'managed_file',
    '#title' => t('Image upload'),
    '#upload_validators' => array(
      'file_validate_extensions' => array(0 => 'png jpg jpeg gif'),
      'file_validate_image_resolution' => array('64x64', '10x10')
    '#upload_location' => 'public://default_images/',
    '#description' => t("Upload an image."),

This is sort of working: it allows me to upload a file and it goes to sites/default/files/default_images, but its only temporary. I've been going through the form api reference and it says I need to permanently save it. I am just unsure how where to put this logic. Do I add a #process key to my file field and call some private function? Or do I create a submit handler? The API reference doesn't go into as much detail as I'd like.

  • 1
    Drupal 7 already has what you are asking for: it's an image field, which allows you to upload an image on the server, and show it though a field widget.
    – apaderno
    Apr 20, 2011 at 0:18
  • 1
    @kiamlaluno I'm not sure if what I just did is what you're talking about, but I'm open to any suggestions.
    – Joe
    Apr 20, 2011 at 0:55

1 Answer 1


You need to add a FILE_STATUS_PERMANENT to the status of the file on form submit then save the file.

$file->status = FILE_STATUS_PERMANENT;

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.