8.5.x file.inc file_create_url($uri)
8.0.x file.inc file_create_url($uri)
8.1.x file.inc file_create_url($uri)
8.2.x file.inc file_create_url($uri)
8.3.x file.inc file_create_url($uri)
8.4.x file.inc file_create_url($uri)
8.6.x file.inc file_create_url($uri)
4.6.x file.inc file_create_url($path)
4.7.x file.inc file_create_url($path)
5.x file.inc file_create_url($path)
6.x file.inc file_create_url($path)
7.x file.inc file_create_url($uri)

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.


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.

See also



Related topics

81 calls to file_create_url()
AjaxCssForm::generateResponse in core/modules/ckeditor/tests/modules/src/Form/AjaxCssForm.php
Generates an AJAX response to add CSS to a CKEditor Text Editor instance.
AttachedAssetsTest::testAddFiles in core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php
Tests adding a CSS and a JavaScript file.
AttachedAssetsTest::testAddJsFileWithQueryString in core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php
Tests JavaScript files that have querystrings attached get added right.
AttachedAssetsTest::testAggregatedAttributes in core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php
Tests that attributes are maintained when JS aggregation is enabled.
AttachedAssetsTest::testAttributes in core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php
Tests adding JavaScript files with additional attributes.

... See full list

1 string reference to 'file_create_url'
CKEditor::buildContentsCssJSSetting in core/modules/ckeditor/src/Plugin/Editor/CKEditor.php
Builds the "contentsCss" configuration part of the CKEditor JS settings.


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


function file_create_url($uri) {

  // Allow the URI to be altered, e.g. to serve a file from a CDN or static
  // file server.
    ->alter('file_url', $uri);
  $scheme = \Drupal::service('file_system')
  if (!$scheme) {

    // Allow for:
    // - root-relative URIs (e.g. /foo.jpg in http://example.com/foo.jpg)
    // - protocol-relative URIs (e.g. //bar.jpg, which is expanded to
    //   http://example.com/bar.jpg by the browser when viewing a page over
    //   HTTP and to https://example.com/bar.jpg when viewing a HTTPS page)
    // Both types of relative URIs are characterized by a leading slash, hence
    // we can use a single check.
    if (Unicode::substr($uri, 0, 1) == '/') {
      return $uri;
    else {

      // If this is not a properly formatted stream, then it is a shipped file.
      // Therefore, return the urlencoded URI with the base URL prepended.
      $options = UrlHelper::parse($uri);
      $path = $GLOBALS['base_url'] . '/' . UrlHelper::encodePath($options['path']);

      // Append the query.
      if ($options['query']) {
        $path .= '?' . UrlHelper::buildQuery($options['query']);

      // Append fragment.
      if ($options['fragment']) {
        $path .= '#' . $options['fragment'];
      return $path;
  elseif ($scheme == 'http' || $scheme == 'https' || $scheme == 'data') {

    // Check for HTTP and data URI-encoded URLs so that we don't have to
    // implement getExternalUrl() for the HTTP and data schemes.
    return $uri;
  else {

    // Attempt to return an external URL using the appropriate wrapper.
    if ($wrapper = \Drupal::service('stream_wrapper_manager')
      ->getViaUri($uri)) {
      return $wrapper
    else {
      return FALSE;