You are here

public function HttpMimeTypeGuesser::parseFileNameFromUrl in Remote Stream Wrapper 8

Parse a file name from a URI.

This also requires the filename to have an extension.

Parameters

string $uri: The URI.

Return value

string|false The filename if it could be parsed from the URI, or FALSE otherwise.

1 call to HttpMimeTypeGuesser::parseFileNameFromUrl()
HttpMimeTypeGuesser::guess in src/File/MimeType/HttpMimeTypeGuesser.php
Guesses the mime type of the file with the given path.

File

src/File/MimeType/HttpMimeTypeGuesser.php, line 87

Class

HttpMimeTypeGuesser
Makes possible to guess the MIME type of a remote file.

Namespace

Drupal\remote_stream_wrapper\File\MimeType

Code

public function parseFileNameFromUrl($uri) {

  // Extract the path part from the URL, ignoring query strings or fragments.
  if ($path = parse_url($uri, PHP_URL_PATH)) {
    $filename = $this->fileSystem
      ->basename($path);

    // Filename must contain a period in order to find a valid extension.
    // If the filename does not contain an extension, then guess() will
    // always return the default 'application/octet-stream' value.
    if (strpos($filename, '.') > 0) {
      return $filename;
    }
  }
  return FALSE;
}