I need to get the path for public:// at runtime. Is there a function I can use to get it?
4 Answers
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();
// ...
}
This code is the simplified version of the code used by file_create_url()
, with the following differences:
- It doesn't allow third-party modules to alter the URL, since the OP is asking for the path associated with public://
- It doesn't check the URI contains ://, since this is clearly true
- It doesn't check the URI is a root-relative URI, since this is clearly false for public://
- It doesn't check the URI is protocol relative, since this is clearly false in the OP's case
- It doesn't check the URI is not a properly formatted stream, since public:// is a properly formatted stream
- It doesn't check if the scheme is http or https, since in this case is public
- It doesn't call
$wrapper->getExternalUrl()
since the OP is asking for the path, not for the URL
If the OP wanted the URL for public://, I would have used the following code, which is still a simplified version of the code used by the Drupal function, which needs to be more generic.
if ($wrapper = file_stream_wrapper_get_instance_by_uri('public://')) {
$url = $wrapper->getExternalUrl();
}
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.– DrCordJul 29, 2014 at 21:05 -
3
In case if you are using only local files (not remote!) you can also try drupal_realpath()
$path = 'public://custom_map';
drupal_realpath($path);
It will return:
D:\Work\OpenServer\domains\local.testsite.com\sites\default\files\custom_map
-
4This returns a path and not a URL, which is what the question asks for. In my opinion this is better than the accepted answer. May 11, 2014 at 19:28
-
api.drupal.org/api/drupal/includes!file.inc/function/… . drupal_realpath() is deprecated as seen in the comments. Nov 25, 2014 at 6:11
-
1@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. Nov 28, 2014 at 13:47
-
1
-
1@Wordzilla: The docs don't say it's deprecated; they said it's discouraged because it only handles local file paths (which the public filesystem would be). From the docs: "Only use this function if you know that the stream wrapper in the URI uses the local file system, and you need to pass an absolute path to a function that is incompatible with stream URIs." Nov 7, 2018 at 23:01
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);
}
or:
$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 in all cases (as the other answers suggest), because the returned URL 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 simpler 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?
Related: