1

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

2

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');
$stream->getExternalUrl();
0

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.

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.