function url, function file_create_url

Public

https://api.drupal.org/api/drupal/includes%21common.inc/function/url/7

function url

7 common.inc url($path = NULL, array $options = array())
4.6 common.inc url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE)
4.7 common.inc url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE)
5 common.inc url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE)
6 common.inc url($path = NULL, $options = array())
Generates an internal or external URL.

When creating links in modules, consider whether l() could be a better alternative than url().

Parameters

$path: (optional) The internal path or external URL being linked to, such as "node/34" or "http://example.com/foo". The default value is equivalent to passing in '<front>'. A few notes:

If you provide a full URL, it will be considered an external URL.
If you provide only the path (e.g. "node/34"), it will be considered an internal link. In this case, it should be a system URL, and it will be replaced with the alias, if one exists. Additional query arguments for internal paths must be supplied in $options['query'], not included in $path.
If you provide an internal path and $options['alias'] is set to TRUE, the path is assumed already to be the correct path alias, and the alias is not looked up.
The special string '<front>' generates a link to the site's base URL.
If your external URL contains a query (e.g. http://example.com/foo?a=b), then you can either URL encode the query keys and values yourself and include them in $path, or use $options['query'] to let this function URL encode them.
$options: (optional) An associative array of additional options, with the following elements:

'query': An array of query key/value-pairs (without any URL-encoding) to append to the URL.
'fragment': A fragment identifier (named anchor) to append to the URL. Do not include the leading '#' character.
'absolute': Defaults to FALSE. Whether to force the output to be an absolute link (beginning with http:). Useful for links that will be displayed outside the site, such as in an RSS feed.
'alias': Defaults to FALSE. Whether the given path is a URL alias already.
'external': Whether the given path is an external URL.
'language': An optional language object. If the path being linked to is internal to the site, $options['language'] is used to look up the alias for the URL. If $options['language'] is omitted, the global $language_url will be used.
'https': Whether this URL should point to a secure location. If not defined, the current scheme is used, so the user stays on HTTP or HTTPS respectively. TRUE enforces HTTPS and FALSE enforces HTTP, but HTTPS can only be enforced when the variable 'https' is set to TRUE.
'base_url': Only used internally, to modify the base URL when a language dependent URL requires so.
'prefix': Only used internally, to modify the path when a language dependent URL requires so.
'script': The script filename in Drupal's root directory to use when clean URLs are disabled, such as 'index.php'. Defaults to an empty string, as most modern web servers automatically find 'index.php'. If clean URLs are disabled, the value of $path is appended as query parameter 'q' to $options['script'] in the returned URL. When deploying Drupal on a web server that cannot be configured to automatically find index.php, then hook_url_outbound_alter() can be implemented to force this value to 'index.php'.
'entity_type': The entity type of the object that called url(). Only set if url() is invoked by entity_uri().
'entity': The entity object (such as a node) for which the URL is being generated. Only set if url() is invoked by entity_uri().
Return value

A string containing a URL to the given path.

301 calls to url()
68 string references to 'url'
File

includes/common.inc, line 2207
Common functions that many Drupal modules will need to reference.

</> CopyGet raw version
php
  1. function url($path = NULL, array $options = array()) {
  2. // Merge in defaults.
  3. $options += array(
  4. 'fragment' => '',
  5. 'query' => array(),
  6. 'absolute' => FALSE,
  7. 'alias' => FALSE,
  8. 'prefix' => ''
  9. );
  10.  
  11. // A duplicate of the code from url_is_external() to avoid needing another
  12. // function call, since performance inside url() is critical.
  13. if (!isset($options ['external'])) {
  14. // Return an external link if $path contains an allowed absolute URL. Avoid
  15. // calling drupal_strip_dangerous_protocols() if there is any slash (/),
  16. // hash (#) or question_mark (?) before the colon (:) occurrence - if any -
  17. // as this would clearly mean it is not a URL. If the path starts with 2
  18. // slashes then it is always considered an external URL without an explicit
  19. // protocol part.
  20. $colonpos = strpos($path, ':');
  21. $options ['external'] = (strpos($path, '//') === 0)
  22. || ($colonpos !== FALSE
  23. && !preg_match('![/?#]!', substr($path, 0, $colonpos))
  24. && drupal_strip_dangerous_protocols($path) == $path);
  25. }
  26.  
  27. // Preserve the original path before altering or aliasing.
  28. $original_path = $path;
  29.  
  30. // Allow other modules to alter the outbound URL and options.
  31. drupal_alter('url_outbound', $path, $options, $original_path);
  32.  
  33. if (isset($options ['fragment']) && $options ['fragment'] !== '') {
  34. $options ['fragment'] = '#' . $options ['fragment'];
  35. }
  36.  
  37. if ($options ['external']) {
  38. // Split off the fragment.
  39. if (strpos($path, '#') !== FALSE) {
  40. list($path, $old_fragment) = explode('#', $path, 2);
  41. // If $options contains no fragment, take it over from the path.
  42. if (isset($old_fragment) && !$options ['fragment']) {
  43. $options ['fragment'] = '#' . $old_fragment;
  44. }
  45. }
  46. // Append the query.
  47. if ($options ['query']) {
  48. $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($options ['query']);
  49. }
  50. if (isset($options ['https']) && variable_get('https', FALSE)) {
  51. if ($options ['https'] === TRUE) {
  52. $path = str_replace('http://', 'https://', $path);
  53. }
  54. elseif ($options ['https'] === FALSE) {
  55. $path = str_replace('https://', 'http://', $path);
  56. }
  57. }
  58. // Reassemble.
  59. return $path . $options ['fragment'];
  60. }
  61.  
  62. // Strip leading slashes from internal paths to prevent them becoming external
  63. // URLs without protocol. /example.com should not be turned into
  64. // //example.com.
  65. $path = ltrim($path, '/');
  66.  
  67. global $base_url, $base_secure_url, $base_insecure_url;
  68.  
  69. // The base_url might be rewritten from the language rewrite in domain mode.
  70. if (!isset($options ['base_url'])) {
  71. if (isset($options ['https']) && variable_get('https', FALSE)) {
  72. if ($options ['https'] === TRUE) {
  73. $options ['base_url'] = $base_secure_url;
  74. $options ['absolute'] = TRUE;
  75. }
  76. elseif ($options ['https'] === FALSE) {
  77. $options ['base_url'] = $base_insecure_url;
  78. $options ['absolute'] = TRUE;
  79. }
  80. }
  81. else {
  82. $options ['base_url'] = $base_url;
  83. }
  84. }
  85.  
  86. // The special path '<front>' links to the default front page.
  87. if ($path == '<front>') {
  88. $path = '';
  89. }
  90. elseif (!empty($path) && !$options ['alias']) {
  91. $language = isset($options ['language']) && isset($options ['language']->language) ? $options ['language']->language : '';
  92. $alias = drupal_get_path_alias($original_path, $language);
  93. if ($alias != $original_path) {
  94. $path = $alias;
  95. }
  96. }
  97.  
  98. $base = $options ['absolute'] ? $options ['base_url'] . '/' : base_path();
  99. $prefix = empty($path) ? rtrim($options ['prefix'], '/') : $options ['prefix'];
  100.  
  101. // With Clean URLs.
  102. if (!empty($GLOBALS ['conf']['clean_url'])) {
  103. $path = drupal_encode_path($prefix . $path);
  104. if ($options ['query']) {
  105. return $base . $path . '?' . drupal_http_build_query($options ['query']) . $options ['fragment'];
  106. }
  107. else {
  108. return $base . $path . $options ['fragment'];
  109. }
  110. }
  111. // Without Clean URLs.
  112. else {
  113. $path = $prefix . $path;
  114. $query = array();
  115. if (!empty($path)) {
  116. $query ['q'] = $path;
  117. }
  118. if ($options ['query']) {
  119. // We do not use array_merge() here to prevent overriding $path via query
  120. // parameters.
  121. $query += $options ['query'];
  122. }
  123. $query = $query ? ('?' . drupal_http_build_query($query)) : '';
  124. $script = isset($options ['script']) ? $options ['script'] : '';
  125. return $base . $script . $query . $options ['fragment'];
  126. }
  127. }

function file_create_url

https://api.drupal.org/api/drupal/includes!file.inc/function/file_create...

7 file.inc file_create_url($uri)
4.6 file.inc file_create_url($path)
4.7 file.inc file_create_url($path)
5 file.inc file_create_url($path)
6 file.inc file_create_url($path)
8 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.
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

File interface
Common file handling functions.
35 calls to file_create_url()
File

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

</> CopyGet raw version
php
  1. function file_create_url($uri) {
  2. // Allow the URI to be altered, e.g. to serve a file from a CDN or static
  3. // file server.
  4. drupal_alter('file_url', $uri);
  5.  
  6. $scheme = file_uri_scheme($uri);
  7.  
  8. if (!$scheme) {
  9. // Allow for:
  10. // - root-relative URIs (e.g. /foo.jpg in http://example.com/foo.jpg)
  11. // - protocol-relative URIs (e.g. //bar.jpg, which is expanded to
  12. // http://example.com/bar.jpg by the browser when viewing a page over
  13. // HTTP and to https://example.com/bar.jpg when viewing a HTTPS page)
  14. // Both types of relative URIs are characterized by a leading slash, hence
  15. // we can use a single check.
  16. if (drupal_substr($uri, 0, 1) == '/') {
  17. return $uri;
  18. }
  19. else {
  20. // If this is not a properly formatted stream, then it is a shipped file.
  21. // Therefore, return the urlencoded URI with the base URL prepended.
  22. return $GLOBALS ['base_url'] . '/' . drupal_encode_path($uri);
  23. }
  24. }
  25. elseif ($scheme == 'http' || $scheme == 'https') {
  26. // Check for HTTP so that we don't have to implement getExternalUrl() for
  27. // the HTTP wrapper.
  28. return $uri;
  29. }
  30. else {
  31. // Attempt to return an external URL using the appropriate wrapper.
  32. if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
  33. return $wrapper->getExternalUrl();
  34. }
  35. else {
  36. return FALSE;
  37. }
  38. }
  39. }
  40.  
  41. // Expecting this function to behave more closely to how it did in Drupal 6? Try combining it with file_build_uri() like this:
  42.  
  43. $file_uri = file_create_url(file_build_uri('images/example.jpeg'));