4

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

1
  • 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
    Commented Oct 1, 2012 at 10:14

3 Answers 3

8

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)

6
  • 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
    Commented Oct 1, 2012 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
    Commented Oct 1, 2012 at 10:36
  • It gives me: http://C:\Users\Me\AppData\Local\Temp/sites/default/files/test.txt
    – Codium
    Commented Oct 1, 2012 at 11:08
  • And is that the right path to your public files folder?
    – Clive
    Commented Oct 1, 2012 at 11:10
  • This is path to temporary directory (C:\Users\Me\AppData\Local\Temp) from admin/config/media/file-system page
    – Codium
    Commented Oct 1, 2012 at 11:20
2

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.

1
0

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

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

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.