file_create_url

  1. drupal
    1. 5
    2. 6
    3. 7
    4. 8
Versions
5 – 6 file_create_url($path)
8 – 7 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.

Parameters

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

Return value

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

http://drupal.org/node/515192

Related topics

▾ 36 functions call file_create_url()

ColorTestCase::_testColor in modules/color/color.test
Tests color module functionality using the given theme.
DrupalTestCase::verbose in modules/simpletest/drupal_web_test_case.php
Logs verbose message in a text file.
drupal_build_css_cache in includes/common.inc
Aggregates and optimizes CSS files into a cache file in the files directory.
drupal_get_js in includes/common.inc
Returns a themed presentation of all JavaScript code for the current page.
drupal_pre_render_styles in includes/common.inc
#pre_render callback to add the elements needed for CSS tags to be rendered.
FileDownloadTest::checkUrl in modules/simpletest/tests/file.test
Download a file from the URL generated by file_create_url().
FileDownloadTest::testPrivateFileTransfer in modules/simpletest/tests/file.test
Test the private file transfer system.
FileDownloadTest::testPublicFileTransfer in modules/simpletest/tests/file.test
Test the public file transfer system.
FileFieldWidgetTestCase::testPrivateFileComment in modules/file/tests/file.test
Tests that download restrictions on private files work on comments.
FileFieldWidgetTestCase::testPrivateFileSetting in modules/file/tests/file.test
Tests a file field with a "Private files" upload destination setting.
FileFieldWidgetTestCase::testSingleValuedWidget in modules/file/tests/file.test
Tests upload and remove buttons, with and without Ajax, for a single-valued File field.
FilePrivateTestCase::testPrivateFile in modules/file/tests/file.test
Uploads a file to a private node, then tests that access is allowed and denied when appropriate.
FileTokenReplaceTestCase::testFileTokenReplacement in modules/file/tests/file.test
Creates a file, then tests the tokens generated from it.
FileURLRewritingTest::testPublicCreatedFileURL in modules/simpletest/tests/file.test
Test the generating of rewritten public created file URLs.
FileURLRewritingTest::testShippedFileURL in modules/simpletest/tests/file.test
Test the generating of rewritten shipped file URLs.
file_example_managed_write_submit in examples/file_example/file_example.module
Submit handler to write a managed file.
file_example_read_submit in examples/file_example/file_example.module
Submit handler for reading a stream wrapper.
file_example_unmanaged_php_submit in examples/file_example/file_example.module
file_example_unmanaged_write_submit in examples/file_example/file_example.module
file_field_formatter_view in modules/file/file.field.inc
Implements hook_field_formatter_view().
ImageFieldDisplayTestCase::_testImageFieldFormatters in modules/image/image.test
Test image formatters on node display.
image_field_formatter_view in modules/image/image.field.inc
Implements hook_field_formatter_view().
image_style_url in modules/image/image.module
Return the URL for an image derivative given a style and image path.
ImportOPMLTestCase::submitImportForm in modules/aggregator/aggregator.test
Submit form with invalid, empty and valid OPML files.
ImportOPMLTestCase::validateImportFormFields in modules/aggregator/aggregator.test
Submit form filled with invalid fields.
rdf_preprocess_field in modules/rdf/rdf.module
Implements MODULE_preprocess_HOOK().
RetrieveFileTestCase::testFileRetrieving in modules/system/system.test
Invokes system_retrieve_file() in several scenarios.
SimpleTestFunctionalTest::getTestResults in modules/simpletest/simpletest.test
Get the results from a test and store them in the class array $results.
system_tokens in modules/system/system.tokens.inc
Implements hook_tokens().
theme_file_link in modules/file/file.module
Returns HTML for a link to a file.
theme_get_setting in includes/theme.inc
Retrieve a setting for the current theme or for a given theme.
theme_image in includes/theme.inc
Returns HTML for an image.
theme_image_button in includes/form.inc
Returns HTML for an image button form element.
theme_image_style_preview in modules/image/image.admin.inc
Returns HTML for a preview of an image style.
UserPictureTestCase::testExternalPicture in modules/user/user.test
Test HTTP schema working with user pictures.
_color_page_alter in modules/color/color.module
Callback for the theme to alter the resources used.

Code

includes/file.inc, line 379

<?php
function file_create_url($uri) {
  // Allow the URI to be altered, e.g. to serve a file from a CDN or static
  // file server.
  drupal_alter('file_url', $uri);

  $scheme = file_uri_scheme($uri);

  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 (drupal_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.
      return $GLOBALS['base_url'] . '/' . drupal_encode_path($uri);
    }
  }
  elseif ($scheme == 'http' || $scheme == 'https') {
    // Check for http so that we don't have to implement getExternalUrl() for
    // the http wrapper.
    return $uri;
  }
  else {
    // Attempt to return an external URL using the appropriate wrapper.
    if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
      return $wrapper->getExternalUrl();
    }
    else {
      return FALSE;
    }
  }
}
?>