1

I have created a custom form using (drupal form API), Is it possible to get the absolute file of the file which will be selected in the file upload field ?

like if user chooses to upload file from "/home/user2/Desktop/something.jpg"

How do I grab that absolute path of the file?

here is my code below,

function example_form($form, &$form_state) {
  $form = array();

  // Use the #managed_file FAPI element to upload a file.
  $form['file_fid'] = array(
    '#title' => t('File'),
    '#type' => 'managed_file',
    '#description' => t('The uploaded file will be processed.'),
    '#default_value' => variable_get('file_fid', ''),
    '#upload_validators' => array(
    'file_validate_extensions' => array('mp3'),
    ),
  '#upload_location' => 'public://songs/',
  '#process' => array('example_my_file_element_process')
);

  $form['buttons']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Import'),
  );

  return $form;
}

/* It disables the default upload button that comes with this #managed_file form */
function example_my_file_element_process($element, &$form_state, $form) {
  $element = file_managed_file_process($element, $form_state, $form);
  $element['upload_button']['#access'] = FALSE;

return $element;
}

function example_form_submit(&$form, &$form_state) {
  // Load the file.
  $file = file_load($form_state['values']['file_fid']);
  $url = file_create_url($file->uri); //here I can get the URI for file
}

I am using Drupal 7 on UBUNTU 12.04

7
  • 1
    Do you mean the absolute path to a file on the user's machine? If so then no, absolutely not - that would be a terrible security hole. If that's not what you mean, could you clarify what you're actually asking for? You already use file_create_url which will get the absolute path to the image on your web server, what are you missing?
    – Clive
    Feb 27, 2014 at 12:07
  • I am working with Sound Cloud api, while uploading file ,the api needs the absolute path of the file. how it is possible ? any suggestion? developers.soundcloud.com/docs#uploading
    – shrish
    Feb 27, 2014 at 12:11
  • 1
    Sorry if I'm misunderstanding, but are you asking how to provide soundcloud with an absolute URL to a file located on a user's private machine? That (thankfully) isn't possible.
    – Clive
    Feb 27, 2014 at 12:15
  • 3
    shrish, probably you'll need the local path of the computer where SounCloud is being run. So, if your Drupal site uses the SoundCloud API your users must upload the file to your Drupal server and then you use the Souncloud API to upload that file already in your server to SoundCloud, so the path you want is the path of the file uploaded to your server.
    – sanzante
    Feb 27, 2014 at 12:27
  • Yeah, that would make a lot more sense @tunic :) And makes this a pretty good question, not sure I've seen the same thing asked here before
    – Clive
    Feb 27, 2014 at 12:34

0

Your Answer

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

Browse other questions tagged or ask your own question.