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://')?

  • Question - what PHP version are you using? it is strongly recommended not to use drupal_realpath(), but use alternative for it. I would suggest you the alternative, but it's not clear what are you trying to do.
    – 32i
    Oct 1 '12 at 10:14

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)

  • Thanks for info, anyway I have still the same (wrong) path. Under admin/config/media/file-system I have public path set to: sites/default/files
    – Codium
    Oct 1 '12 at 10:30
  • Very strange; what sort of path do you get if you try a test like this: $path = file_create_url('public://test.txt');?
    – Clive
    Oct 1 '12 at 10:36
  • It gives me: http://C:\Users\Me\AppData\Local\Temp/sites/default/files/test.txt
    – Codium
    Oct 1 '12 at 11:08
  • And is that the right path to your public files folder?
    – Clive
    Oct 1 '12 at 11:10
  • This is path to temporary directory (C:\Users\Me\AppData\Local\Temp) from admin/config/media/file-system page
    – Codium
    Oct 1 '12 at 11:20

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.


Drupal uses PHP's file stream and the path is configured in this location


You can change it to where ever you want

  • It is set to sites/default/files so it look ok
    – Codium
    Oct 1 '12 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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