file_create_url

Versions
4.6 – 6
file_create_url($path)
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.

See also

http://drupal.org/node/515192

There are two kinds of local files:

  • "created files", i.e. those in the files directory (which is stored in the file_directory_path variable and can be retrieved using file_directory_path()). 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', nothing is done and the same string is returned. If a valid stream wrapper could not be found to generate an external URL, then FALSE will be returned.

Related topics

▾ 17 functions call file_create_url()

drupal_get_css in includes/common.inc
Returns a themed representation of all stylesheets that should be attached to the page.
drupal_get_js in includes/common.inc
Returns a themed presentation of all JavaScript code for the current page.
garland_get_ie_styles in themes/garland/template.php
Generates IE CSS links for LTR and RTL languages.
image_style_url in modules/image/image.module
Return the URL for an image derivative given a style and image path.
system_tokens in modules/system/system.tokens.inc
Implement hook_tokens().
theme_field_formatter_file_url_plain in modules/file/file.field.inc
Theme function for 'url_plain' file field formatter.
theme_field_formatter_image_link_file in modules/image/image.field.inc
Theme function for 'image_link_file' image field formatter.
theme_file_link in modules/file/file.module
Output 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
Return a themed image.
theme_image_button in includes/form.inc
Theme a image button form element.
theme_image_style in modules/image/image.module
Return a themed image using a specific image style.
theme_image_style_preview in modules/image/image.admin.inc
Theme callback for displaying a preview of an image style.
theme_upload_attachments in modules/upload/upload.module
Displays file attachments in table
upload_node_view in modules/upload/upload.module
Implement hook_node_view().
upload_tokens in modules/upload/upload.tokens.inc
Implement hook_tokens().
_upload_form in modules/upload/upload.module

Code

includes/file.inc, line 310

<?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) {
    // If this is not a properly formatted stream, then it is a shipped file.
    // Therefor, return the URI with the base URL prepended.
    return $GLOBALS['base_url'] . '/' . $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;
    }
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.