if I have a file saved in private:://somefile.txt , what function can I use in drupal to give me the external URL to access that?

I tried PrivateStream::getExternalUrl() but I get this error

PHP Fatal error: Call to undefined method Drupal\mymodule\myservice::getTarget() in /opt/DrupalSites/drupal-wcms-migration/docroot/core/lib/Drupal/Core/StreamWrapper/PrivateStream.php on line 50


You can't call PrivateStream::getExternalUrl() directly because it's not a static function; it needs an instantiated stream before you can call getExternalUrl() on it.

You can retrieve it via the stream wrapper manager's getViaUri() method:

$stream = \Drupal::service('stream_wrapper_manager')->getViaUri('private:://somefile.txt');
| improve this answer | |

You don't have to call getExternalUrl() directly either, you should just use file_create_url('private://somefile.txt').

There is no OOP version of it yet, this is being worked on.

| improve this answer | |

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.