function ExtensionStreamBase::checkFileExtension

Checks that the given URI has an allowed file extension.

This checks the `stream_wrapper.allowed_file_extensions` container parameter, which lists all file extensions allowed for different URI schemes. If there is no list for the given scheme, then the file is assumed to be disallowed.

Parameters

string $uri: A URI to check.

Throws

\InvalidArgumentException Thrown if the given URI has a file extension that is not allowed by the container parameter.

2 calls to ExtensionStreamBase::checkFileExtension()
ExtensionStreamBase::getTarget in core/lib/Drupal/Core/StreamWrapper/ExtensionStreamBase.php
Returns the local writable target of the resource within the stream.
ExtensionStreamBase::setUri in core/lib/Drupal/Core/StreamWrapper/ExtensionStreamBase.php
Sets the absolute stream resource URI.

File

core/lib/Drupal/Core/StreamWrapper/ExtensionStreamBase.php, line 125

Class

ExtensionStreamBase
Defines a base stream wrapper implementation for extension assets.

Namespace

Drupal\Core\StreamWrapper

Code

protected function checkFileExtension(string $uri) : void {
  [$scheme] = explode('://', $uri, 2);
  $allowed = \Drupal::getContainer()->getParameter('stream_wrapper.allowed_file_extensions');
  $extension = pathinfo($uri, PATHINFO_EXTENSION);
  if (isset($allowed[$scheme]) && in_array(strtolower($extension), $allowed[$scheme], TRUE)) {
    return;
  }
  throw new \InvalidArgumentException("The {$scheme} stream wrapper does not support the '{$extension}' file type.");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.