You are here

function file_create_url in Drupal 9

Same name and namespace in other branches
  1. 8 core/includes/file.inc \file_create_url()
  2. 4 includes/file.inc \file_create_url()
  3. 5 includes/file.inc \file_create_url()
  4. 6 includes/file.inc \file_create_url()
  5. 7 includes/file.inc \file_create_url()

Creates a web-accessible URL for a stream to an external or local file.

Compatibility: normal paths and stream wrappers.

There are two kinds of local files:

  • "managed files", i.e. those stored by a Drupal-compatible stream wrapper. These are files that have either been uploaded by users or were generated automatically (for example through CSS aggregation).
  • "shipped files", i.e. those outside of the files directory, which ship as part of Drupal core or contributed modules or themes.

Parameters

string $uri: The URI to a file for which we need an external URL, or the path to a shipped file.

Return value

string A string containing a URL that may be used to access the file. If the provided string already contains a preceding 'http', 'https', or '/', nothing is done and the same string is returned. If a stream wrapper could not be found to generate an external URL, then FALSE is returned.

Deprecated

in drupal:9.3.0 and is removed from drupal:10.0.0. Use the appropriate method on \Drupal\Core\File\FileUrlGeneratorInterface instead.

See also

https://www.drupal.org/node/2940031

https://www.drupal.org/node/515192

\Drupal\Core\File\FileUrlGeneratorInterface::generate()

\Drupal\Core\File\FileUrlGeneratorInterface::generateString()

\Drupal\Core\File\FileUrlGeneratorInterface::generateAbsoluteString()

\Drupal\Core\File\FileUrlGeneratorInterface::transformRelative()

Related topics

1 call to file_create_url()
FileSystemDeprecationTest::testDeprecatedFileCreateUrl in core/tests/Drupal/KernelTests/Core/File/FileSystemDeprecationTest.php
Tests deprecated FileCreateUrl.

File

core/includes/file.inc, line 66
API for handling file uploads and server file management.

Code

function file_create_url($uri) {
  @trigger_error('file_create_url() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use the appropriate method on \\Drupal\\Core\\File\\FileUrlGeneratorInterface instead. See https://www.drupal.org/node/2940031', E_USER_DEPRECATED);
  try {
    return \Drupal::service('file_url_generator')
      ->generateAbsoluteString($uri);
  } catch (InvalidStreamWrapperException $e) {
    return FALSE;
  }
}