4

As in questions. I am calling drupal_realpath('public://') inside module function. I have no idea why I am getting path like: sites/all/modules/current_module/sites/default/files, e.g.

C:\drupal_path\sites\all\modules[current_module]\sites\default\files

  • 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
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)

| improve this answer | |
  • 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
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.

| improve this answer | |
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

| improve this answer | |
  • 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.