Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I need to get the path for public:// at runtime. Is there a fx call to resolve it?


4 Answers 4

up vote 16 down vote accepted

You can use the following code, to obtain the real path of public://.

if ($wrapper = file_stream_wrapper_get_instance_by_uri('public://')) {
  $realpath = $wrapper->realpath();
@junedkazi's is a better answer. –  AlessMascherpa Apr 29 '14 at 9:08
Read the code used by file_create_url(); you will see the executed code, in this case, is exactly the one I wrote in my answer. –  kiamlaluno Apr 29 '14 at 9:13
Thanks @kiamlaluno. That proves my point. –  AlessMascherpa Apr 29 '14 at 11:26
this will get you the full server path, not the url. –  DrCord Jul 29 '14 at 21:04
@DrCord It gives you the same thing that file_create_url() gives you. –  kiamlaluno Jul 30 '14 at 6:28

You can use the file_create_url function.

$uri = 'public://';
$path= file_create_url($uri)
This gets you the url, but including the server. I used a simple regex and the global $base_url to remove it and make this server agnostic. global $base_url; $path = file_create_url($file_object->uri); $path = preg_replace('#' . $base_url . '#', '', $path); Make sure to use the '#' in the regex, otherwise the replace will fail due to an un-escaped '/' in the $base_url. –  DrCord Jul 29 '14 at 21:05

In case if you are using only local files (not remote!) you can also try drupal_realpath()

$path = 'public://custom_map';

It will return:

This returns a path and not a URL, which is what the question asks for. In my opinion this is better than the accepted answer. –  Scott Joudry May 11 '14 at 19:28
api.drupal.org/api/drupal/includes!file.inc/function/… . drupal_realpath() is deprecated as seen in the comments. –  Wordzilla Nov 25 '14 at 6:11
@Wordzilla, well it's not. If you will check latest Drupal source you will find it's usage in big amount of places, it's not deprecated but it's usage must be restricted. Documentation in file.inc says that it is "discouraged", but not deprecated. –  TracKer Nov 28 '14 at 13:47
@ScottJoudry The OP asks for the path for public://, not the URL. –  kiamlaluno Mar 24 at 9:31

See the following examples:

$scheme = file_uri_scheme($file);

if ($scheme && file_stream_wrapper_valid_scheme($scheme)) {
  $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
  $path = $wrapper->getLocalPath($file);


$wrapper = file_stream_wrapper_get_instance_by_uri($uri);
$path = $wrapper->getDirectoryPath() . "/" . file_uri_target($uri);
print $path;

Check API for DrupalPublicStreamWrapper.

Using file_create_url() doesn't work (as the other answers suggest), because the URL returned doesn't have to be on the same server. And using $_SERVER['DOCUMENT_ROOT'] works only for certain server configurations.

If you don't want to use stream wrappers, you may try the simplest solution:

$path = variable_get('file_public_path', conf_path() . '/files') . '/' . file_uri_target($uri);

Otherwise if you need something that works more generally, check @hannanxp's solution:

$wrapper = file_stream_wrapper_get_instance_by_uri($uri);
if ($wrapper instanceof DrupalLocalStreamWrapper) {
  $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
else {
  // This does not appear to be a local file so there's no such thing as
  // a relative path; do appropriate error handling here.

Read more: How can I convert a file uri to relative path?


Why drupal_realpath('public://') returns sites/all/modules/current_module/sites/default/files path?

How do I get the public url of a file from the uri or filename


Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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