function drupal_basename

You are here

7 drupal_basename($uri, $suffix = NULL)
8 drupal_basename($uri, $suffix = NULL)

Gets the filename from a given path.

PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character.

See also


Related topics

37 calls to drupal_basename()
color_css_alter in core/modules/color/color.module
Implements hook_css_alter().
color_scheme_form_submit in core/modules/color/color.module
Form submission handler for color_scheme_form().
drupal_tempnam in core/includes/
Creates a file with a unique filename in the specified directory.
EditorFileUsageTest::testEditorEntityHooks in core/modules/editor/lib/Drupal/editor/Tests/EditorFileUsageTest.php
Tests the configurable text editor manager.
File::preCreate in core/modules/file/lib/Drupal/file/Entity/File.php
Changes the values of an entity before it is created.

... See full list


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


function drupal_basename($uri, $suffix = NULL) {
  $separators = '/';
    // For Windows OS add special separator.
    $separators .= DIRECTORY_SEPARATOR;
  // Remove right-most slashes when $uri points to directory.
  $uri = rtrim($uri, $separators);
  // Returns the trailing part of the $uri starting after one of the directory
  // separators.
  $filename = preg_match('@[^' . preg_quote($separators, '@') . ']+$@', $uri, $matches) ? $matches[0] : '';
  // Cuts off a suffix from the filename.
  if ($suffix) {
    $filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename);
  return $filename;