This is my form to upload a photo

 $form['Background_image'] = array(
 '#type' => 'managed_file',
 '#title' => t('Choose a background image'),
 '#description' => t('Click "Browse..." to select an image to upload.'),
 '#required' => TRUE,
 '#upload_validators' => array('file_validate_extensions' => array('jpeg jpg png gif')),
'#upload_location' => 'public://backgroundimage/',
 '#default_value' => $this->options['Background_image'],


I tried this function that get the current uploaded image and return its path, but still not working: what is missing:

function image_path()
   $f = file_load($this->options['Background_image']);
  //this too is not working: 
  //$f = file_load($form_state['values']['Background_image']);
   $url_image = file_create_url($f->uri);
   return ($url_image);
  • can you check if you are getting the value of $f->uri, if not than can you try $f = $form_state['values']['Background_image']; Oct 8, 2013 at 11:34
  • Whats $this in your current context?
    – Clive
    Oct 8, 2013 at 11:51

2 Answers 2


$this is surprising in your form! I am not sure but do not consider $this as reference of current uploaded file,if the code you have shared is complete code then if you try to look inside dpm($this) , you will get your answer why file is not loading. check this link, it explains how to create custom upload form.

the use of file_create_url() is appropriate to get file url from uri,so I am sharing the submit handler changed codes from reference link the submit handler showing use of

function import_form_submit(&$form, &$form_state) {
  // Load the file.
  $file = file_load($form_state['values']['file_fid']);
  // Change status to permanent.
  $file->status = FILE_STATUS_PERMANENT;
  // Save.
  $uploaded = file_save($file);
  if ($uploaded == TRUE) {
    drupal_set_message(t('The file has been uploaded.'));
  else {
    drupal_set_message(t('The file could not be uploaded. Please contact the site administrator.'), 'error');

  • when I tried drupal_set_message(t( dpm(file_create_url($file->uri))));to see the file path but drupal dispays:localhost:8080/TestD3 (link of my site) Oct 8, 2013 at 12:34
  • please do debugging and use dpm() without drupal_set_messgage() and t(), it looks like you are able to upload the file properly , check the upload location you are providing in the form field.
    – arpitr
    Oct 8, 2013 at 12:54

In your submit handler of your form, you should be able to retrieve the fid of the uploaded file / image. By using this fid, you can load the file and work with that object.

function YOUR_MODULE_FORM_NAME_submit($form, &$form_state)
    $fid = $form_state['values']['Background_image'];

    if ($fid !== 0) {
        // Retrieve image URL
        $image_url = YOUR_MODULE_get_file_url(file_load($fid));

function YOUR_MODULE_get_file_url($file)
    if (!$file) {
        // File not found
        return false;

    // Return URL from a give file URI
    return file_create_url($file->uri);

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.