I am calling drupal_realpath('public://')
inside a module function.
Why am I getting a path like sites/all/modules/current_module/sites/default/files from drupal_realpath('public://')
?
I am calling drupal_realpath('public://')
inside a module function.
Why am I getting a path like sites/all/modules/current_module/sites/default/files from drupal_realpath('public://')
?
drupal_realpath()
has been deprecated and shouldn't be used; since you have a stream wrapper URI you should be able to get the correct path like this:
$uri = 'public://';
if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
$path = $wrapper->realpath();
}
Also make sure that your public path is correct in the UI (at admin/config/media/file-system)
$path = file_create_url('public://test.txt');
?
In addition to Clive's anwser using:
$path = $wrapper->getExternalUrl();
Instead of using:
$path = $wrapper->realpath();
Worked for me, I had the same problem with the wrong path.
$wrapper->getExternalUrl()
returns a URL. Only DrupalPublicStreamWrapper::getExternalUrl()
returns a relative URL for the site running Drupal, which can then be used as path, but other stream wrappers could return an absolute URL which cannot be used as path, as DrupalPrivateStreamWrapper::getExternalUrl()
does.
Drupal uses PHP's file stream and the path is configured in this location
admin/config/media/file-system
You can change it to where ever you want