I've created a formular where the user can upload an image using a file field. In another formular the user is able to change the image he previous uploaded.

So far I was able that the user can upload a new picture or let the field empty and the existing image remains. My question is, how do I show the user in a nice way which file he previously uploaded with the field? Setting a #default_value (@code below) does nothing for me.

//$smallimage = fid of image
$form['smallimage'] = array(
  '#type' => 'file',
  '#title' => t('Image'),
  '#default_value' => $smallimage,
  '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),

Has anyone a solution/workaround without switching to managed_file?

Thank you in advance


As seen in the answers there is no direct solution for the problem. So I've changed the type back to managed_file and gave user1 and node1 the responsibility of being the reference of my files. I'm not happy with that but it works.

  • 1
    Could you explain why you don't want to use managed files? – Mołot Oct 28 '13 at 10:02
  • My module doesn't create any "type" like a node or a user so I had a problem using the file_add_usage()-function which I need to update a managed file. – FeBe Oct 28 '13 at 10:07
  • My modules does not do that too, but when I have one file per user, I key it by module name, file's "role", and uid. – Mołot Oct 28 '13 at 10:09
  • I thought of that solution too, but in my module the administrator has a unknown number of pictures. – FeBe Oct 28 '13 at 10:12
  • I have this, too, in one place. I just add additional number to them :P – Mołot Oct 28 '13 at 10:45

File element only have these properties:

#access, #after_build, #array_parents, #attached, #attributes, #description, #disabled, #element_validate, #parents, #post_render, #prefix, #pre_render, #process, #required, #size (default: 60), #states, #suffix, #theme, #theme_wrappers, #title, #title_display, #tree, #type, #weight

As you can see, no #value or #default_value there. If you want to manage files, managed file element is there for you. If you insist on not using them, simply display old image as image, and add tip like this to your file element on the second screen:

Use this field to upload image that will be used to replace image above.

Now, if you have a problem with file_usage_add(stdClass $file, $module, $type, $id, $count = 1) - the fact that your module "doesn't create any "type" like a node or a user" Should not be a problem. Just use your form id as $type, and auto increment $id, or use any other means - you are identifying these files somehow already, that's or sure.


No, there's no way to do this. The restriction is with HTML, not Drupal. A file input simply doesn't have the capacity for a default value to be set; allowing such behaviour would introduce a security hole (imagine you pre-filled it with /~/.ssh/id_rsa, auto-submitted the form, and the user didn't have permissions set up correctly). See this post.

If you check the FAPI guide you'll see there's no #default_value available for a file field type.

You'll need to switch to a managed_file element if you need a default value for the field.


If you really don't want to use managed_file, what about providing that info under description:

$form['smallimage'] = array(
  '#type' => 'file',
  '#title' => t('Image'),
  '#description' => t('Current file: '.drupal_basename($smallimage) .' Update file, allowed extensions: jpg, jpeg, png, gif'),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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