20

I have trouble to get the upload directroy from drupal. Is there some function returning the relative or absolute path?

2
  • 1
    Consider using wrappers (public://, private://, etc to avoid any folder conflicts. Take advantage of Drupal 7 :) )
    – AKS
    Commented Feb 12, 2013 at 19:39
  • I know that this works with image_style_url(uri) but i want to pass it to javascript as a variable for futher processing...thanks though
    – Jurudocs
    Commented Feb 12, 2013 at 20:03

3 Answers 3

24

This information is saved in Drupal's variables table. Use the variable_get function to get this information, passing in the name of the correct variable:

  • Public Filesystem Location: variable_get('file_public_path', conf_path() . '/files');
  • Private Filesystem Location: variable_get('file_private_path');

If you want the temporary upload directory use variable_get('file_temporary_path'); If you want to determine the system specific upload directory, regardless of the actual site configuration use the file_directory_temp function.

variable_get('file_public_path', conf_path() . '/files'); will pull the default path for you in case it has not been modified or explicitly set by the site administrator.

4
  • 1
    hmm in my case variable_get('file_public_path') is empty...do i have to call it within a hook or so?
    – Jurudocs
    Commented Feb 12, 2013 at 19:59
  • 1
    @Jurudocs Use this instead: variable_get('file_public_path', conf_path() . '/files'); Basically, it pulls the default path for you in case it has not been modified or explicitly set by the site administrator.
    – 9ee1
    Commented Feb 12, 2013 at 20:03
  • @zhilevan Check your file permissions on that directory.
    – Atomox
    Commented Oct 8, 2014 at 19:16
  • This only gives the relative path which seems less useful than the absolute path. I like the drupal_realpath solution below. Seems more legit. Commented Sep 21, 2017 at 17:03
11

To get the absolute file system paths use drupal_realpath (Drupal API) instead of file_create_url:

drupal_realpath('public://'); 
// returns e.g. /var/www/drupal/sites/default/files

drupal_realpath('private://');
// returns e.g. /var/www/drupal/sites/default/files_private
3

You can also use these functions to get a valid url from a relative uri:

file_create_url('public://');
file_create_url('private://');

They return the path to the public or private folders.

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.